Class ObjectReader
- java.lang.Object
-
- org.codehaus.jackson.ObjectCodec
-
- org.codehaus.jackson.map.ObjectReader
-
- All Implemented Interfaces:
Versioned
public class ObjectReader extends ObjectCodec implements Versioned
Builder object that can be used for per-serialization configuration of deserialization parameters, such as root type to use or object to update (instead of constructing new instance). Uses "fluid" (aka builder) pattern so that instances are immutable (and thus fully thread-safe with no external synchronization); new instances are constructed for different configurations. Instances are initially constructed byObjectMapperand can be reused.- Since:
- 1.6
- Author:
- tatu
-
-
Field Summary
Fields Modifier and Type Field Description protected DeserializationConfig_configGeneral serialization configuration settings; while immutable, can use copy-constructor to create modified instances as necessary.protected InjectableValues_injectableValuesValues that can be injected during deserialization, if any.protected JsonFactory_jsonFactoryFactory used for constructingJsonGeneratorsprotected DeserializerProvider_providerprotected java.util.concurrent.ConcurrentHashMap<JavaType,JsonDeserializer<java.lang.Object>>_rootDeserializersRoot-level cached deserializersprotected FormatSchema_schemaWhen using data format that uses a schema, schema is passed to parser.protected boolean_unwrapRootFlag that indicates whether root values are expected to be unwrapped or notprotected java.lang.Object_valueToUpdateInstance to update with data binding; if any.protected JavaType_valueTypeDeclared type of value to instantiate during deserialization.
-
Constructor Summary
Constructors Modifier Constructor Description protectedObjectReader(ObjectMapper mapper, DeserializationConfig config)Constructor used byObjectMapperfor initial instantiationprotectedObjectReader(ObjectMapper mapper, DeserializationConfig config, JavaType valueType, java.lang.Object valueToUpdate, FormatSchema schema, InjectableValues injectableValues)protectedObjectReader(ObjectReader base, DeserializationConfig config, JavaType valueType, java.lang.Object valueToUpdate, FormatSchema schema, InjectableValues injectableValues)Copy constructor used for building variations.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected java.lang.Object_bind(JsonParser jp)Actual implementation of value reading+binding operation.protected java.lang.Object_bindAndClose(JsonParser jp)protected JsonNode_bindAndCloseAsTree(JsonParser jp)protected JsonNode_bindAsTree(JsonParser jp)protected DeserializationContext_createDeserializationContext(JsonParser jp, DeserializationConfig cfg)protected JsonDeserializer<java.lang.Object>_findRootDeserializer(DeserializationConfig cfg, JavaType valueType)Method called to locate deserializer for the passed root-level value.protected static JsonToken_initForReading(JsonParser jp)protected java.lang.Object_unwrapAndDeserialize(JsonParser jp, DeserializationContext ctxt, JavaType rootType, JsonDeserializer<java.lang.Object> deser)JsonNodecreateArrayNode()Method for construct root level Array nodes for Tree Model instances.JsonNodecreateObjectNode()Method for construct root level Object nodes for Tree Model instances.JsonNodereadTree(java.io.InputStream in)Method that reads content from given input source, using configuration of this reader, and binds it as JSON Tree.JsonNodereadTree(java.io.Reader r)Method that reads content from given input source, using configuration of this reader, and binds it as JSON Tree.JsonNodereadTree(java.lang.String content)Method that reads content from given JSON input String, using configuration of this reader, and binds it as JSON Tree.JsonNodereadTree(JsonParser jp)Convenience method that binds content read using given parser, using configuration of this reader, except that content is bound as JSON tree instead of configured root value type.<T> TreadValue(byte[] src)Method that binds content read from given byte array, using configuration of this reader.<T> TreadValue(byte[] src, int offset, int length)Method that binds content read from given byte array, using configuration of this reader.<T> TreadValue(java.io.File src)<T> TreadValue(java.io.InputStream src)Method that binds content read from given input source, using configuration of this reader.<T> TreadValue(java.io.Reader src)Method that binds content read from given input source, using configuration of this reader.<T> TreadValue(java.lang.String src)Method that binds content read from given JSON string, using configuration of this reader.<T> TreadValue(java.net.URL src)Method that binds content read from given input source, using configuration of this reader.<T> TreadValue(JsonNode src)Convenience method for converting results from given JSON tree into given value type.<T> TreadValue(JsonParser jp)Method that binds content read using given parser, using configuration of this reader, including expected result type.<T> TreadValue(JsonParser jp, java.lang.Class<T> valueType)Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type).<T> TreadValue(JsonParser jp, JavaType valueType)Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type).<T> TreadValue(JsonParser jp, TypeReference<?> valueTypeRef)Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type).<T> MappingIterator<T>readValues(byte[] src)Since 1.9.3<T> MappingIterator<T>readValues(byte[] src, int offset, int length)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(java.io.File src)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(java.io.InputStream src)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(java.io.Reader src)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(java.lang.String json)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(java.net.URL src)Method for reading sequence of Objects from parser stream.<T> MappingIterator<T>readValues(JsonParser jp)Method for reading sequence of Objects from parser stream.<T> java.util.Iterator<T>readValues(JsonParser jp, java.lang.Class<T> valueType)Convenience method that is equivalent to:<T> java.util.Iterator<T>readValues(JsonParser jp, JavaType valueType)Convenience method that is equivalent to:<T> java.util.Iterator<T>readValues(JsonParser jp, TypeReference<?> valueTypeRef)Convenience method that is equivalent to:JsonParsertreeAsTokens(JsonNode n)Method for constructing aJsonParserfor reading contents of a JSON tree, as if it was external serialized JSON content.<T> TtreeToValue(JsonNode n, java.lang.Class<T> valueType)Convenience method for converting given JSON tree into instance of specified value type.Versionversion()Method that will return version information stored in and read from jar that contains this class.ObjectReaderwithInjectableValues(InjectableValues injectableValues)Method for constructing a new instance with configuration that uses passedInjectableValuesto provide injectable values.ObjectReaderwithNodeFactory(JsonNodeFactory f)Method for constructing a new reader instance with configuration that uses passedJsonNodeFactoryfor constructingJsonNodeinstances.ObjectReaderwithSchema(FormatSchema schema)Method for constructing a new instance with configuration that passes specifiedFormatSchematoJsonParserthat is constructed for parsing content.ObjectReaderwithType(java.lang.Class<?> valueType)Method for constructing a new reader instance that is configured to data bind into specified type.ObjectReaderwithType(java.lang.reflect.Type valueType)Method for constructing a new reader instance that is configured to data bind into specified type.ObjectReaderwithType(JavaType valueType)Method for constructing a new reader instance that is configured to data bind into specified type.ObjectReaderwithType(TypeReference<?> valueTypeRef)Method for constructing a new reader instance that is configured to data bind into specified type.ObjectReaderwithValueToUpdate(java.lang.Object value)Method for constructing a new instance with configuration that updates passed Object (as root value), instead of constructing a new value.voidwriteTree(JsonGenerator jgen, JsonNode rootNode)NOTE: NOT implemented forObjectReader.voidwriteValue(JsonGenerator jgen, java.lang.Object value)Method to serialize given Java Object, using generator provided.
-
-
-
Field Detail
-
_config
protected final DeserializationConfig _config
General serialization configuration settings; while immutable, can use copy-constructor to create modified instances as necessary.
-
_unwrapRoot
protected final boolean _unwrapRoot
Flag that indicates whether root values are expected to be unwrapped or not- Since:
- 1.9
-
_rootDeserializers
protected final java.util.concurrent.ConcurrentHashMap<JavaType,JsonDeserializer<java.lang.Object>> _rootDeserializers
Root-level cached deserializers
-
_provider
protected final DeserializerProvider _provider
-
_jsonFactory
protected final JsonFactory _jsonFactory
Factory used for constructingJsonGenerators
-
_valueType
protected final JavaType _valueType
Declared type of value to instantiate during deserialization. Defines which deserializer to use; as well as base type of instance to construct if an updatable value is not configured to be used (subject to changes by embedded type information, for polymorphic types). If_valueToUpdateis non-null, only used for locating deserializer.
-
_valueToUpdate
protected final java.lang.Object _valueToUpdate
Instance to update with data binding; if any. If null, a new instance is created, if non-null, properties of this value object will be updated instead. Note that value can be of almost any type, except notArrayType; array types can not be modified because array size is immutable.
-
_schema
protected final FormatSchema _schema
When using data format that uses a schema, schema is passed to parser.- Since:
- 1.8
-
_injectableValues
protected final InjectableValues _injectableValues
Values that can be injected during deserialization, if any.- Since:
- 1.9
-
-
Constructor Detail
-
ObjectReader
protected ObjectReader(ObjectMapper mapper, DeserializationConfig config)
Constructor used byObjectMapperfor initial instantiation- Since:
- 1.8
-
ObjectReader
protected ObjectReader(ObjectMapper mapper, DeserializationConfig config, JavaType valueType, java.lang.Object valueToUpdate, FormatSchema schema, InjectableValues injectableValues)
-
ObjectReader
protected ObjectReader(ObjectReader base, DeserializationConfig config, JavaType valueType, java.lang.Object valueToUpdate, FormatSchema schema, InjectableValues injectableValues)
Copy constructor used for building variations.
-
-
Method Detail
-
version
public Version version()
Method that will return version information stored in and read from jar that contains this class.
-
withType
public ObjectReader withType(JavaType valueType)
Method for constructing a new reader instance that is configured to data bind into specified type.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withType
public ObjectReader withType(java.lang.Class<?> valueType)
Method for constructing a new reader instance that is configured to data bind into specified type.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withType
public ObjectReader withType(java.lang.reflect.Type valueType)
Method for constructing a new reader instance that is configured to data bind into specified type.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withType
public ObjectReader withType(TypeReference<?> valueTypeRef)
Method for constructing a new reader instance that is configured to data bind into specified type.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
- Since:
- 1.8
-
withNodeFactory
public ObjectReader withNodeFactory(JsonNodeFactory f)
Method for constructing a new reader instance with configuration that uses passedJsonNodeFactoryfor constructingJsonNodeinstances.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withValueToUpdate
public ObjectReader withValueToUpdate(java.lang.Object value)
Method for constructing a new instance with configuration that updates passed Object (as root value), instead of constructing a new value.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
-
withSchema
public ObjectReader withSchema(FormatSchema schema)
Method for constructing a new instance with configuration that passes specifiedFormatSchematoJsonParserthat is constructed for parsing content.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
- Since:
- 1.8
-
withInjectableValues
public ObjectReader withInjectableValues(InjectableValues injectableValues)
Method for constructing a new instance with configuration that uses passedInjectableValuesto provide injectable values.Note that the method does NOT change state of this reader, but rather construct and returns a newly configured instance.
- Since:
- 1.9
-
readValue
public <T> T readValue(JsonParser jp) throws java.io.IOException, JsonProcessingException
Method that binds content read using given parser, using configuration of this reader, including expected result type. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(JsonParser jp, java.lang.Class<T> valueType) throws java.io.IOException, JsonProcessingException
Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type). Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Specified by:
readValuein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(JsonParser jp, TypeReference<?> valueTypeRef) throws java.io.IOException, JsonProcessingException
Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type). Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Specified by:
readValuein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(JsonParser jp, JavaType valueType) throws java.io.IOException, JsonProcessingException
Convenience method that binds content read using given parser, using configuration of this reader, except that expected value type is specified with the call (instead of currently configured root type). Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Specified by:
readValuein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readTree
public JsonNode readTree(JsonParser jp) throws java.io.IOException, JsonProcessingException
Convenience method that binds content read using given parser, using configuration of this reader, except that content is bound as JSON tree instead of configured root value type.Note: if an object was specified with
withValueToUpdate(java.lang.Object), it will be ignored.- Specified by:
readTreein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValues
public <T> java.util.Iterator<T> readValues(JsonParser jp, java.lang.Class<T> valueType) throws java.io.IOException, JsonProcessingException
Convenience method that is equivalent to:withType(valueType).readValues(jp);
- Specified by:
readValuesin classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValues
public <T> java.util.Iterator<T> readValues(JsonParser jp, TypeReference<?> valueTypeRef) throws java.io.IOException, JsonProcessingException
Convenience method that is equivalent to:withType(valueTypeRef).readValues(jp);
- Specified by:
readValuesin classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValues
public <T> java.util.Iterator<T> readValues(JsonParser jp, JavaType valueType) throws java.io.IOException, JsonProcessingException
Convenience method that is equivalent to:withType(valueType).readValues(jp);
- Specified by:
readValuesin classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(java.io.InputStream src) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given input source, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(java.io.Reader src) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given input source, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(java.lang.String src) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given JSON string, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(byte[] src) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given byte array, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(byte[] src, int offset, int length) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given byte array, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(java.io.File src) throws java.io.IOException, JsonProcessingException- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(java.net.URL src) throws java.io.IOException, JsonProcessingExceptionMethod that binds content read from given input source, using configuration of this reader. Value return is either newly constructed, or root value that was specified withwithValueToUpdate(Object).- Throws:
java.io.IOExceptionJsonProcessingException
-
readValue
public <T> T readValue(JsonNode src) throws java.io.IOException, JsonProcessingException
Convenience method for converting results from given JSON tree into given value type. Basically short-cut for:objectReader.readValue(src.traverse())
- Throws:
java.io.IOExceptionJsonProcessingException
-
readTree
public JsonNode readTree(java.io.InputStream in) throws java.io.IOException, JsonProcessingException
Method that reads content from given input source, using configuration of this reader, and binds it as JSON Tree.Note that if an object was specified with a call to
withValueToUpdate(Object)it will just be ignored; result is always a newly constructedJsonNodeinstance.- Throws:
java.io.IOExceptionJsonProcessingException
-
readTree
public JsonNode readTree(java.io.Reader r) throws java.io.IOException, JsonProcessingException
Method that reads content from given input source, using configuration of this reader, and binds it as JSON Tree.Note that if an object was specified with a call to
withValueToUpdate(Object)it will just be ignored; result is always a newly constructedJsonNodeinstance.- Throws:
java.io.IOExceptionJsonProcessingException
-
readTree
public JsonNode readTree(java.lang.String content) throws java.io.IOException, JsonProcessingException
Method that reads content from given JSON input String, using configuration of this reader, and binds it as JSON Tree.Note that if an object was specified with a call to
withValueToUpdate(Object)it will just be ignored; result is always a newly constructedJsonNodeinstance.- Throws:
java.io.IOExceptionJsonProcessingException
-
readValues
public <T> MappingIterator<T> readValues(JsonParser jp) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public <T> MappingIterator<T> readValues(java.io.InputStream src) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public <T> MappingIterator<T> readValues(java.io.Reader src) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public <T> MappingIterator<T> readValues(java.lang.String json) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public <T> MappingIterator<T> readValues(byte[] src, int offset, int length) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public final <T> MappingIterator<T> readValues(byte[] src) throws java.io.IOException, JsonProcessingException
Since 1.9.3- Throws:
java.io.IOExceptionJsonProcessingException
-
readValues
public <T> MappingIterator<T> readValues(java.io.File src) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
readValues
public <T> MappingIterator<T> readValues(java.net.URL src) throws java.io.IOException, JsonProcessingException
Method for reading sequence of Objects from parser stream.- Throws:
java.io.IOExceptionJsonProcessingException- Since:
- 1.8
-
_bind
protected java.lang.Object _bind(JsonParser jp) throws java.io.IOException, JsonParseException, JsonMappingException
Actual implementation of value reading+binding operation.- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
_bindAndClose
protected java.lang.Object _bindAndClose(JsonParser jp) throws java.io.IOException, JsonParseException, JsonMappingException
- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
_bindAsTree
protected JsonNode _bindAsTree(JsonParser jp) throws java.io.IOException, JsonParseException, JsonMappingException
- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
_bindAndCloseAsTree
protected JsonNode _bindAndCloseAsTree(JsonParser jp) throws java.io.IOException, JsonParseException, JsonMappingException
- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
_initForReading
protected static JsonToken _initForReading(JsonParser jp) throws java.io.IOException, JsonParseException, JsonMappingException
- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
_findRootDeserializer
protected JsonDeserializer<java.lang.Object> _findRootDeserializer(DeserializationConfig cfg, JavaType valueType) throws JsonMappingException
Method called to locate deserializer for the passed root-level value.- Throws:
JsonMappingException
-
_createDeserializationContext
protected DeserializationContext _createDeserializationContext(JsonParser jp, DeserializationConfig cfg)
-
_unwrapAndDeserialize
protected java.lang.Object _unwrapAndDeserialize(JsonParser jp, DeserializationContext ctxt, JavaType rootType, JsonDeserializer<java.lang.Object> deser) throws java.io.IOException, JsonParseException, JsonMappingException
- Throws:
java.io.IOExceptionJsonParseExceptionJsonMappingException
-
createArrayNode
public JsonNode createArrayNode()
Description copied from class:ObjectCodecMethod for construct root level Array nodes for Tree Model instances.- Specified by:
createArrayNodein classObjectCodec
-
createObjectNode
public JsonNode createObjectNode()
Description copied from class:ObjectCodecMethod for construct root level Object nodes for Tree Model instances.- Specified by:
createObjectNodein classObjectCodec
-
treeAsTokens
public JsonParser treeAsTokens(JsonNode n)
Description copied from class:ObjectCodecMethod for constructing aJsonParserfor reading contents of a JSON tree, as if it was external serialized JSON content.- Specified by:
treeAsTokensin classObjectCodec
-
treeToValue
public <T> T treeToValue(JsonNode n, java.lang.Class<T> valueType) throws java.io.IOException, JsonProcessingException
Description copied from class:ObjectCodecConvenience method for converting given JSON tree into instance of specified value type. This is equivalent to first constructing aJsonParserto iterate over contents of the tree, and using that parser for data binding.- Specified by:
treeToValuein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
writeTree
public void writeTree(JsonGenerator jgen, JsonNode rootNode) throws java.io.IOException, JsonProcessingException
NOTE: NOT implemented forObjectReader.- Specified by:
writeTreein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
writeValue
public void writeValue(JsonGenerator jgen, java.lang.Object value) throws java.io.IOException, JsonProcessingException
Description copied from class:ObjectCodecMethod to serialize given Java Object, using generator provided.- Specified by:
writeValuein classObjectCodec- Throws:
java.io.IOExceptionJsonProcessingException
-
-