@Override public DateMidnight deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // We'll accept either long (timestamp) or array: if (jp.isExpectedStartArrayToken()) { jp.nextToken(); // VALUE_NUMBER_INT int year = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int month = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int day = jp.getIntValue(); if (jp.nextToken() != JsonToken.END_ARRAY) { throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after DateMidnight ints"); } return new DateMidnight(year, month, day); } switch (jp.getCurrentToken()) { case VALUE_NUMBER_INT: return new DateMidnight(jp.getLongValue()); case VALUE_STRING: DateTime local = parseLocal(jp); if (local == null) { return null; } return local.toDateMidnight(); } throw ctxt.wrongTokenException( jp, JsonToken.START_ARRAY, "expected JSON Array, Number or String"); }
/** This is the trickiest thing to handle, since property we are looking for may be anywhere... */ @Override public Object deserializeTypedFromObject(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // but first, sanity check to ensure we have START_OBJECT or FIELD_NAME JsonToken t = jp.getCurrentToken(); if (t == JsonToken.START_OBJECT) { t = jp.nextToken(); } else if (t != JsonToken.FIELD_NAME) { throw ctxt.wrongTokenException( jp, JsonToken.START_OBJECT, "need JSON Object to contain As.PROPERTY type information (for class " + baseTypeName() + ")"); } // Ok, let's try to find the property. But first, need token buffer... TokenBuffer tb = null; for (; t == JsonToken.FIELD_NAME; t = jp.nextToken()) { String name = jp.getCurrentName(); jp.nextToken(); // to point to the value if (_propertyName.equals(name)) { // gotcha! JsonDeserializer<Object> deser = _findDeserializer(ctxt, jp.getText()); // deserializer should take care of closing END_OBJECT as well if (tb != null) { jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp); } /* Must point to the next value; tb had no current, jp * pointed to VALUE_STRING: */ jp.nextToken(); // to skip past String value // deserializer should take care of closing END_OBJECT as well return deser.deserialize(jp, ctxt); } if (tb == null) { tb = new TokenBuffer(null); } tb.writeFieldName(name); tb.copyCurrentStructure(jp); } // Error if we get here... throw ctxt.wrongTokenException( jp, JsonToken.FIELD_NAME, "missing property '" + _propertyName + "' that is to contain type id (for class " + baseTypeName() + ")"); }
public Object deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // Ok: must point to START_ARRAY if (jp.getCurrentToken() != JsonToken.START_ARRAY) { /* 04-Oct-2009, tatu: One exception; byte arrays are generally * serialized as base64, so that should be handled */ if (jp.getCurrentToken() == JsonToken.VALUE_STRING && _elementClass == Byte.class) { return deserializeFromBase64(jp, ctxt); } throw ctxt.mappingException(_arrayType.getRawClass()); } final ObjectBuffer buffer = ctxt.leaseObjectBuffer(); Object[] chunk = buffer.resetAndStart(); int ix = 0; JsonToken t; final TypeDeserializer typeDeser = _elementTypeDeserializer; while ((t = jp.nextToken()) != JsonToken.END_ARRAY) { // Note: must handle null explicitly here; value deserializers won't Object value; if (t == JsonToken.VALUE_NULL) { value = null; } else if (typeDeser == null) { value = _elementDeserializer.deserialize(jp, ctxt); } else { value = _elementDeserializer.deserializeWithType(jp, ctxt, typeDeser); } if (ix >= chunk.length) { chunk = buffer.appendCompletedChunk(chunk); ix = 0; } chunk[ix++] = value; } Object[] result; if (_untyped) { result = buffer.completeAndClearBuffer(chunk, ix); } else { result = buffer.completeAndClearBuffer(chunk, ix, _elementClass); } ctxt.returnObjectBuffer(buffer); return result; }
@Override public Calendar deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { Date d = _parseDate(jp, ctxt); if (d == null) { return null; } if (_calendarClass == null) { return ctxt.constructCalendar(d); } try { Calendar c = _calendarClass.newInstance(); c.setTimeInMillis(d.getTime()); return c; } catch (Exception e) { throw ctxt.instantiationException(_calendarClass, e); } }
@Override public JavaType deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken curr = jp.getCurrentToken(); // Usually should just get string value: if (curr == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); if (str.length() == 0) { return getEmptyValue(); } return ctxt.getTypeFactory().constructFromCanonical(str); } // or occasionally just embedded object maybe if (curr == JsonToken.VALUE_EMBEDDED_OBJECT) { return (JavaType) jp.getEmbeddedObject(); } throw ctxt.mappingException(_valueClass); }
@Override public Share deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { ObjectMapper mapper = new ObjectMapper(); mapper.setDeserializationConfig(ctxt.getConfig()); jp.setCodec(mapper); return mapper.readValue(jp.readValueAsTree().get("share"), new TypeReference<Share>() {}); }
protected Byte[] deserializeFromBase64(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // First same as what ArrayDeserializers.ByteDeser does: byte[] b = jp.getBinaryValue(ctxt.getBase64Variant()); // But then need to convert to wrappers Byte[] result = new Byte[b.length]; for (int i = 0, len = b.length; i < len; ++i) { result[i] = Byte.valueOf(b[i]); } return result; }
@Override public LocalDateTime deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // We'll accept either long (timestamp) or array: if (jp.isExpectedStartArrayToken()) { jp.nextToken(); // VALUE_NUMBER_INT int year = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int month = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int day = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int hour = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int minute = jp.getIntValue(); jp.nextToken(); // VALUE_NUMBER_INT int second = jp.getIntValue(); // let's leave milliseconds optional? int millisecond = 0; if (jp.nextToken() != JsonToken.END_ARRAY) { // VALUE_NUMBER_INT millisecond = jp.getIntValue(); jp.nextToken(); // END_ARRAY? } if (jp.getCurrentToken() != JsonToken.END_ARRAY) { throw ctxt.wrongTokenException(jp, JsonToken.END_ARRAY, "after LocalDateTime ints"); } return new LocalDateTime(year, month, day, hour, minute, second, millisecond); } switch (jp.getCurrentToken()) { case VALUE_NUMBER_INT: return new LocalDateTime(jp.getLongValue()); case VALUE_STRING: DateTime local = parseLocal(jp); if (local == null) { return null; } return local.toLocalDateTime(); } throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Array or Number"); }
@Override public Map<Object, Object> deserialize( JsonParser jp, DeserializationContext ctxt, Map<Object, Object> result) throws IOException, JsonProcessingException { // Ok: must point to START_OBJECT or FIELD_NAME JsonToken t = jp.getCurrentToken(); if (t != JsonToken.START_OBJECT && t != JsonToken.FIELD_NAME) { throw ctxt.mappingException(getMapClass()); } _readAndBind(jp, ctxt, result); return result; }
@Override public ReadablePeriod deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { // TODO: perhaps support array of numbers... // if (jp.isExpectedStartArrayToken()) { ] switch (jp.getCurrentToken()) { case VALUE_NUMBER_INT: // assume it's millisecond count return new Period(jp.getLongValue()); case VALUE_STRING: return new Period(jp.getText()); } throw ctxt.wrongTokenException(jp, JsonToken.START_ARRAY, "expected JSON Number or String"); }
@Override public Map<Object, Object> 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 && t != JsonToken.FIELD_NAME) { throw ctxt.mappingException(getMapClass()); } if (_propertyBasedCreator != null) { return _deserializeUsingCreator(jp, ctxt); } Map<Object, Object> result; if (_defaultCtor == null) { throw ctxt.instantiationException(getMapClass(), "No default constructor found"); } try { result = _defaultCtor.newInstance(); } catch (Exception e) { throw ctxt.instantiationException(getMapClass(), e); } _readAndBind(jp, ctxt, result); return result; }
@SuppressWarnings("unchecked") @Override public T deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException, JsonProcessingException { JsonToken t = jp.getCurrentToken(); if (t == JsonToken.VALUE_NUMBER_INT) { return (T) new DateTime(jp.getLongValue(), DateTimeZone.UTC); } if (t == JsonToken.VALUE_STRING) { String str = jp.getText().trim(); if (str.length() == 0) { // [JACKSON-360] return null; } return (T) new DateTime(str, DateTimeZone.UTC); } throw ctxt.mappingException(getValueClass()); }