/** * Method used to create a {@link JsonParser} that can read contents * stored in this buffer. Will use default <code>_objectCodec</code> for * object conversions. *<p> * Note: instances are not synchronized, that is, they are not thread-safe * if there are concurrent appends to the underlying buffer. * * @return Parser that can be used for reading contents stored in this buffer */ public JsonParser asParser() { return asParser(_objectCodec); }
public Object processUnwrapped(JsonParser originalParser, DeserializationContext ctxt, Object bean, TokenBuffer buffered) throws IOException, JsonProcessingException { for (int i = 0, len = _properties.size(); i < len; ++i) { SettableBeanProperty prop = _properties.get(i); JsonParser jp = buffered.asParser(); jp.nextToken(); prop.deserializeAndSet(jp, ctxt, bean); } return bean; } }
protected Object _convert(Object fromValue, JavaType toValueType) throws IllegalArgumentException { // sanity check for null first: if (fromValue == null) return null; /* Then use TokenBuffer, which is a JsonGenerator: * (see [JACKSON-175]) */ TokenBuffer buf = new TokenBuffer(this); try { writeValue(buf, fromValue); // and provide as with a JsonParser for contents as well! JsonParser jp = buf.asParser(); Object result = readValue(jp, toValueType); jp.close(); return result; } catch (IOException e) { // should not occur, no real i/o... throw new IllegalArgumentException(e.getMessage(), e); } }
public static JsonNode toJsonNode(Object datum) { if (datum == null) { return null; } try { TokenBuffer generator = new TokenBuffer(new ObjectMapper()); toJson(datum, generator); return new ObjectMapper().readTree(generator.asParser()); } catch (IOException e) { throw new AvroRuntimeException(e); } }
JsonParser jp = asParser(); int count = 0;
try { writeValue(buf, fromValue); JsonParser jp = buf.asParser(); result = readTree(jp); jp.close();
protected final void _deserialize(JsonParser jp, DeserializationContext ctxt, Object bean, int index) throws IOException, JsonProcessingException { /* Ok: time to mix type id, value; and we will actually use "wrapper-array" * style to ensure we can handle all kinds of JSON constructs. */ TokenBuffer merged = new TokenBuffer(jp.getCodec()); merged.writeStartArray(); merged.writeString(_typeIds[index]); JsonParser p2 = _tokens[index].asParser(jp); p2.nextToken(); merged.copyCurrentStructure(p2); merged.writeEndArray(); // needs to point to START_OBJECT (or whatever first token is) p2 = merged.asParser(jp); p2.nextToken(); _properties[index].getProperty().deserializeAndSet(p2, ctxt, bean); }
/** * Method called to handle set of one or more unknown properties, * stored in their entirety in given {@link TokenBuffer} * (as field entries, name and value). */ protected Object handleUnknownProperties(DeserializationContext ctxt, Object bean, TokenBuffer unknownTokens) throws IOException, JsonProcessingException { // First: add closing END_OBJECT as marker unknownTokens.writeEndObject(); // note: buffer does NOT have starting START_OBJECT JsonParser bufferParser = unknownTokens.asParser(); while (bufferParser.nextToken() != JsonToken.END_OBJECT) { String propName = bufferParser.getCurrentName(); // Unknown: let's call handler method bufferParser.nextToken(); handleUnknownProperty(bufferParser, ctxt, bean, propName); } return bean; }
/** * Method used to create a {@link JsonParser} that can read contents * stored in this buffer. Will use default <code>_objectCodec</code> for * object conversions. *<p> * Note: instances are not synchronized, that is, they are not thread-safe * if there are concurrent appends to the underlying buffer. * * @return Parser that can be used for reading contents stored in this buffer */ public JsonParser asParser() { return asParser(_objectCodec); }
JsonParser p2 = unknownTokens.asParser();
protected Object _deserializeTypedUsingDefaultImpl(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException, JsonProcessingException { // As per [JACKSON-614], may have default implementation to use if (_defaultImpl != null) { JsonDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt); if (tb != null) { tb.writeEndObject(); jp = tb.asParser(jp); // must move to point to the first token: jp.nextToken(); } return deser.deserialize(jp, ctxt); } // or, perhaps we just bumped into a "natural" value (boolean/int/double/String)? Object result = _deserializeIfNatural(jp, ctxt); if (result != null) { return result; } // or, something for which "as-property" won't work, changed into "wrapper-array" type: if (jp.getCurrentToken() == JsonToken.START_ARRAY) { return super.deserializeTypedFromAny(jp, ctxt); } throw ctxt.wrongTokenException(jp, JsonToken.FIELD_NAME, "missing property '"+_typePropertyName+"' that is to contain type id (for class "+baseTypeName()+")"); }
jp = JsonParserSequence.createFlattened(tb.asParser(jp), jp);
public Object processUnwrapped(JsonParser originalParser, DeserializationContext ctxt, Object bean, TokenBuffer buffered) throws IOException, JsonProcessingException { for (int i = 0, len = _properties.size(); i < len; ++i) { SettableBeanProperty prop = _properties.get(i); JsonParser jp = buffered.asParser(); jp.nextToken(); prop.deserializeAndSet(jp, ctxt, bean); } return bean; } }
protected Object _convert(Object fromValue, JavaType toValueType) throws IllegalArgumentException { // sanity check for null first: if (fromValue == null) return null; /* Then use TokenBuffer, which is a JsonGenerator: * (see [JACKSON-175]) */ TokenBuffer buf = new TokenBuffer(this); try { writeValue(buf, fromValue); // and provide as with a JsonParser for contents as well! JsonParser jp = buf.asParser(); Object result = readValue(jp, toValueType); jp.close(); return result; } catch (IOException e) { // should not occur, no real i/o... throw new IllegalArgumentException(e.getMessage(), e); } }
JsonParser jp = asParser(); int count = 0;
try { writeValue(buf, fromValue); JsonParser jp = buf.asParser(); result = readTree(jp); jp.close();
protected final void _deserialize(JsonParser jp, DeserializationContext ctxt, Object bean, int index) throws IOException, JsonProcessingException { /* Ok: time to mix type id, value; and we will actually use "wrapper-array" * style to ensure we can handle all kinds of JSON constructs. */ TokenBuffer merged = new TokenBuffer(jp.getCodec()); merged.writeStartArray(); merged.writeString(_typeIds[index]); JsonParser p2 = _tokens[index].asParser(jp); p2.nextToken(); merged.copyCurrentStructure(p2); merged.writeEndArray(); // needs to point to START_OBJECT (or whatever first token is) p2 = merged.asParser(jp); p2.nextToken(); _properties[index].getProperty().deserializeAndSet(p2, ctxt, bean); }
/** * Method called to handle set of one or more unknown properties, * stored in their entirety in given {@link TokenBuffer} * (as field entries, name and value). */ protected Object handleUnknownProperties(DeserializationContext ctxt, Object bean, TokenBuffer unknownTokens) throws IOException, JsonProcessingException { // First: add closing END_OBJECT as marker unknownTokens.writeEndObject(); // note: buffer does NOT have starting START_OBJECT JsonParser bufferParser = unknownTokens.asParser(); while (bufferParser.nextToken() != JsonToken.END_OBJECT) { String propName = bufferParser.getCurrentName(); // Unknown: let's call handler method bufferParser.nextToken(); handleUnknownProperty(bufferParser, ctxt, bean, propName); } return bean; }
JsonParser p2 = unknownTokens.asParser();
protected Object _deserializeTypedUsingDefaultImpl(JsonParser jp, DeserializationContext ctxt, TokenBuffer tb) throws IOException, JsonProcessingException { // As per [JACKSON-614], may have default implementation to use if (_defaultImpl != null) { JsonDeserializer<Object> deser = _findDefaultImplDeserializer(ctxt); if (tb != null) { tb.writeEndObject(); jp = tb.asParser(jp); // must move to point to the first token: jp.nextToken(); } return deser.deserialize(jp, ctxt); } // or, perhaps we just bumped into a "natural" value (boolean/int/double/String)? Object result = _deserializeIfNatural(jp, ctxt); if (result != null) { return result; } // or, something for which "as-property" won't work, changed into "wrapper-array" type: if (jp.getCurrentToken() == JsonToken.START_ARRAY) { return super.deserializeTypedFromAny(jp, ctxt); } throw ctxt.wrongTokenException(jp, JsonToken.FIELD_NAME, "missing property '"+_typePropertyName+"' that is to contain type id (for class "+baseTypeName()+")"); }