/** * Helper method for constructing generic mapping exception for specified type * * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead */ @Deprecated public JsonMappingException mappingException(Class<?> targetClass) { return mappingException(targetClass, _parser.getCurrentToken()); }
@Override public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_NUMBER_INT) { return DateTimes.utc(jp.getLongValue()); } if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); if (str.length() == 0) { // [JACKSON-360] return null; } // make sure to preserve time zone information when parsing timestamps return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str); } throw ctxt.mappingException(getValueClass()); } }
JsonToken token = parser.nextToken(); if (token != JsonToken.START_ARRAY ) throw deserializationContext.mappingException("Invalid data point syntax."); parser.nextToken(); long timestamp = parser.getLongValue(); parser.nextToken(); String value = parser.getText(); token = parser.nextToken(); if (token != JsonToken.END_ARRAY) throw deserializationContext.mappingException("Invalid data point syntax."); token = parser.nextToken();
private final short[] handleNonArray(JsonParser jp, DeserializationContext ctxt) throws IOException { // [JACKSON-620] Empty String can become null... if ((jp.getCurrentToken() == JsonToken.VALUE_STRING) && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { if (jp.getText().length() == 0) { return null; } } if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) { throw ctxt.mappingException(_valueClass); } return new short[] { _parseShortPrimitive(jp, ctxt) }; } }
@Override public LocalFile deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); jp.nextToken(); result = LocalFile.ofContent(jp.getValueAsString()); } else if ("base64".equals(keyName)) { jp.nextToken(); ByteArrayOutputStream out = new ByteArrayOutputStream(); jp.readBinaryValue(ctxt.getBase64Variant(), out); result = LocalFile.ofContent(out.toByteArray()); } else { throw ctxt.mappingException("Unknown key '" + keyName + "' to deserialize LocalFile"); t = jp.nextToken(); if (t != JsonToken.END_OBJECT) { throw ctxt.mappingException("Unexpected extra map keys to LocalFile"); case START_ARRAY: case END_ARRAY: throw ctxt.mappingException("Attempted unexpected map or array to LocalFile"); return (LocalFile) obj; throw ctxt.mappingException("Don't know how to convert embedded Object of type " + obj.getClass().getName() + " into LocalFile");
@Override public Duration deserialize(JsonParser parser, DeserializationContext context) throws IOException { switch (parser.getCurrentTokenId()) { case JsonTokenId.ID_NUMBER_FLOAT: BigDecimal value = parser.getDecimalValue(); long seconds = value.longValue(); int nanoseconds = DecimalUtils.extractNanosecondDecimal(value, seconds); return Duration.ofSeconds(seconds, nanoseconds); case JsonTokenId.ID_NUMBER_INT: return Duration.ofSeconds(parser.getLongValue()); case JsonTokenId.ID_STRING: String string = parser.getText().trim(); if (string.length() == 0) { return null; } return Duration.parse(string); } throw context.mappingException("Expected type float, integer, or string."); } }
protected void _checkFailOnNumber(DeserializationContext ctxt) throws IOException { if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) { throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)"); } }
private final boolean[] handleNonArray(JsonParser jp, DeserializationContext ctxt) throws IOException { // [JACKSON-620] Empty String can become null... if ((jp.getCurrentToken() == JsonToken.VALUE_STRING) && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { if (jp.getText().length() == 0) { return null; } } if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) { throw ctxt.mappingException(_valueClass); } return new boolean[] { _parseBooleanPrimitive(jp, ctxt) }; } }
@Override public DateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_NUMBER_INT) { return DateTimes.utc(jp.getLongValue()); } if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); if (str.length() == 0) { // [JACKSON-360] return null; } // make sure to preserve time zone information when parsing timestamps return DateTimes.ISO_DATE_OR_TIME_WITH_OFFSET.parse(str); } throw ctxt.mappingException(getValueClass()); } }
String firstKey; JsonToken t = p.getCurrentToken(); } else { if (t != JsonToken.END_OBJECT) { throw ctx.mappingException(handledType(), p.getCurrentToken()); p.nextToken(); resultMap.put(firstKey, deserialize(p, ctx)); p.nextToken(); if (resultMap.containsKey(nextKey)) { Object listObject = resultMap.get(nextKey);
/** * Helper method for constructing generic mapping exception for specified type * * @deprecated Since 2.8 use {@link #handleUnexpectedToken(Class, JsonParser)} instead */ @Deprecated public JsonMappingException mappingException(Class<?> targetClass) { return mappingException(targetClass, _parser.getCurrentToken()); }
String string = parser.getText().trim(); if (string.length() == 0) { return null; throw context.mappingException("Expected type float, integer, or string.");
protected void _checkFailOnNumber(DeserializationContext ctxt) throws IOException { if (ctxt.isEnabled(DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS)) { throw ctxt.mappingException("Not allowed to deserialize Enum value out of JSON number (disable DeserializationConfig.DeserializationFeature.FAIL_ON_NUMBERS_FOR_ENUMS to allow)"); } }
private final long[] handleNonArray(JsonParser jp, DeserializationContext ctxt) throws IOException { // [JACKSON-620] Empty String can become null... if ((jp.getCurrentToken() == JsonToken.VALUE_STRING) && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { if (jp.getText().length() == 0) { return null; } } if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) { throw ctxt.mappingException(_valueClass); } return new long[] { _parseLongPrimitive(jp, ctxt) }; } }
@Override public StackTraceElement deserialize(final JsonParser jp, final DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken(); final String propName = jp.getCurrentName(); if ("class".equals(propName)) { className = jp.getText(); } else if ("file".equals(propName)) { fileName = jp.getText(); } else if ("line".equals(propName)) { if (t.isNumeric()) { methodName = jp.getText(); } else if ("nativeMethod".equals(propName)) { throw ctxt.mappingException(_valueClass, t);
@Override public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // Ok: must point to START_OBJECT or FIELD_NAME JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { // If START_OBJECT, move to next; may also be END_OBJECT t = jp.nextToken(); } if (t != JsonToken.FIELD_NAME && t != JsonToken.END_OBJECT) { throw ctxt.mappingException(_mapType.getRawClass()); } return _deserializeEntries(jp, ctxt); }
protected Object _deserializeFromNonArray(JsonParser p, DeserializationContext ctxt) throws IOException { // Let's start with failure throw ctxt.mappingException("Can not deserialize a POJO (of type "+_beanType.getRawClass().getName() +") from non-Array representation (token: "+p.getCurrentToken() +"): type/property designed to be serialized as JSON Array"); // in future, may allow use of "standard" POJO serialization as well; if so, do: //return _delegate.deserialize(jp, ctxt); } }
public Object nullValue(DeserializationContext ctxt) throws JsonProcessingException { if (_isPrimitive && ctxt.isEnabled(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES)) { throw ctxt.mappingException("Can not map JSON null into type "+_rawType.getName() +" (set DeserializationConfig.DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES to 'false' to allow)"); } return _nullValue; } }
private final int[] handleNonArray(JsonParser jp, DeserializationContext ctxt) throws IOException { // [JACKSON-620] Empty String can become null... if ((jp.getCurrentToken() == JsonToken.VALUE_STRING) && ctxt.isEnabled(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT)) { if (jp.getText().length() == 0) { return null; } } if (!ctxt.isEnabled(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY)) { throw ctxt.mappingException(_valueClass); } return new int[] { _parseIntPrimitive(jp, ctxt) }; } }
public class DateTimeDesrializer extends StdScalarDeserializer<DateTime> { public DateTimeDesrializer() { super(DateTime.class); } @Override public DateTime deserialize(JsonParser jsonParser, DeserializationContext deserializationContext) throws IOException, JsonProcessingException { try { JsonToken currentToken = jsonParser.getCurrentToken(); if (currentToken == JsonToken.VALUE_STRING) { String dateTimeAsString = jsonParser.getText().trim(); return ISODateTimeFormat.withZoneUTC().parseDateTime(dateTimeAsString); } } finally { throw deserializationContext.mappingException(getValueClass()); } }