Package com.cedarsoftware.util.io
Class JsonReader
java.lang.Object
com.cedarsoftware.util.io.JsonReader
- All Implemented Interfaces:
Closeable
,AutoCloseable
Read an object graph in JSON format and make it available in Java objects, or
in a "Map of Maps." (untyped representation). This code handles cyclic references
and can deserialize any Object graph without requiring a class to be 'Serializeable'
or have any specific methods on it. It will handle classes with non-public constructors.
Usages:
Usages:
-
Call the static method:
JsonReader.jsonToJava(String json)
. This will return a typed Java object graph. -
Call the static method:
JsonReader.jsonToMaps(String json)
. This will return an untyped object representation of the JSON String as a Map of Maps, where the fields are the Map keys, and the field values are the associated Map's values. You can call the JsonWriter.objectToJson() method with the returned Map, and it will serialize the Graph into the equivalent JSON stream from which it was read. -
Instantiate the JsonReader with an InputStream:
JsonReader(InputStream in)
and then callreadObject()
. Cast the return value of readObject() to the Java class that was the root of the graph. -
Instantiate the JsonReader with an InputStream:
JsonReader(InputStream in, true)
and then callreadObject()
. The return value will be a Map of Maps.
- Author:
- John DeRegnaucourt ([email protected])
Copyright (c) Cedar Software LLC
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interface
Subclass this interface and create a class that will return a new instance of the passed in Class (c).static class
Use to create new instances of collection interfaces (needed for empty collections)static interface
Implement this interface to add a custom JSON reader.static interface
Common ancestor for JsonClassReaderstatic interface
Implement this interface to add a custom JSON reader.static class
Use to create new instances of Map interfaces (needed for empty Maps).static interface
Used to react to fields missing when reading an object. -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final String
If set, use the specified ClassLoaderstatic final String
If set, this maps class ==> CustomReaderstatic final String
If set, this map will contain Factory classes for creating hard to instantiate objects.static final String
Will fail JSON parsing if 'type' class defined but is not on classpath.static final String
Pointer to 'this' (automatically placed in the Map)static final String
If set, this object will be called when a field is present in the JSON but missing from the corresponding classstatic final String
If set, this indicates that no custom reader should be used for the specified class ==> CustomReaderstatic final String
Pointer to the current ObjectResolver (automatically placed in the Map)static final String
If set, this map will be used when reading @type values - allows shorthand abbreviations type namesstatic final String
What to do when an object is found and 'type' cannot be determined.static final String
If set, the read-in JSON will be turned into a Map of Maps (JsonObject) representation -
Constructor Summary
ConstructorsConstructorDescriptionDeprecated.JsonReader
(byte[] bytes, ReadOptions readOptions) Creates a json reader using custom read optionsJsonReader
(byte[] inp, Map<String, Object> optionalArgs) Deprecated.JsonReader
(byte[] inp, Map<String, Object> optionalArgs, int maxDepth) Deprecated.JsonReader
(int maxDepth) Deprecated.JsonReader
(ReadOptions options) Use this constructor if you already have a JsonObject graph and want to parse it into Java objects by calling jsonReader.jsonObjectsToJava(rootJsonObject) after constructing the JsonReader.JsonReader
(InputStream inputStream) Deprecated.JsonReader
(InputStream inputStream, boolean useMaps) Deprecated.JsonReader
(InputStream inputStream, boolean useMaps, int maxDepth) Deprecated.JsonReader
(InputStream inputStream, int maxDepth) Deprecated.JsonReader
(InputStream inp, ReadOptions readOptions) Creates a json reader using custom read optionsJsonReader
(InputStream inputStream, Map<String, Object> optionalArgs) Deprecated.JsonReader
(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.JsonReader
(String json) Creates a json reader using default read optionsJsonReader
(String json, ReadOptions readOptions) Creates a json reader using custom read optionsJsonReader
(String json, Map<String, Object> optionalArgs) Deprecated.JsonReader
(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.JsonReader
(Map<String, Object> optionalArgs) Deprecated.JsonReader
(Map<String, Object> optionalArgs, int maxDepth) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addNotCustomReader
(Class<?> c) Deprecated.use ReadOptionsBuilder.withNonCustomizableClass()void
addReader
(Class<?> c, JsonReader.JsonClassReader reader) Deprecated.use ReadOptionsBuilder.withCustomReader() to create any additional readers you'll need.static void
assignInstantiator
(Class<?> c, JsonReader.ClassFactory factory) Deprecated.static void
assignInstantiator
(String className, JsonReader.ClassFactory factory) Deprecated.void
close()
<T> T
convertParsedMapsToJava
(JsonObject root, Class<T> hint) This method converts a root Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance.protected FastReader
getReader
(InputStream inputStream) jsonObjectsToJava
(JsonObject root) Deprecated.static <T> T
jsonToJava
(InputStream inputStream, Map<String, Object> optionalArgs) Deprecated.static <T> T
jsonToJava
(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.static <T> T
jsonToJava
(String json) Deprecated.static <T> T
jsonToJava
(String json, Map<String, Object> optionalArgs) Deprecated.static Object
jsonToJava
(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.static Map
jsonToMaps
(InputStream inputStream, Map<String, Object> optionalArgs) Deprecated.static Map
jsonToMaps
(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.static Map
jsonToMaps
(String json) Deprecated.static Map
jsonToMaps
(String json, int maxDepth) Deprecated.static Map
jsonToMaps
(String json, Map<String, Object> optionalArgs) Deprecated.static Map
jsonToMaps
(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Read JSON input from the stream that was set up in the constructor, turning it into Java Maps (JsonObject's).static <T> T
toMaps
(InputStream inputStream, ReadOptions readOptions) Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).static <T> T
Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).static <T> T
toMaps
(String json, ReadOptions readOptions) Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).static <T> T
toObjects
(byte[] bytes, ReadOptions options) Convert the passed in JSON string into a Java object graph.static <T> T
toObjects
(InputStream input, ReadOptions options) Convert the passed in JSON string into a Java object graph.static <T> T
Convert the passed in JSON string into a Java object graph.static <T> T
toObjects
(String jsonString, ReadOptions options) Convert the passed in JSON string into a Java object graph.protected boolean
useMaps()
-
Field Details
-
CUSTOM_READER_MAP
If set, this maps class ==> CustomReader- See Also:
-
NOT_CUSTOM_READER_MAP
If set, this indicates that no custom reader should be used for the specified class ==> CustomReader- See Also:
-
USE_MAPS
If set, the read-in JSON will be turned into a Map of Maps (JsonObject) representation- See Also:
-
UNKNOWN_OBJECT
What to do when an object is found and 'type' cannot be determined.- See Also:
-
FAIL_ON_UNKNOWN_TYPE
Will fail JSON parsing if 'type' class defined but is not on classpath.- See Also:
-
JSON_READER
Pointer to 'this' (automatically placed in the Map)- See Also:
-
OBJECT_RESOLVER
Pointer to the current ObjectResolver (automatically placed in the Map)- See Also:
-
TYPE_NAME_MAP
If set, this map will be used when reading @type values - allows shorthand abbreviations type names- See Also:
-
MISSING_FIELD_HANDLER
If set, this object will be called when a field is present in the JSON but missing from the corresponding class- See Also:
-
CLASSLOADER
If set, use the specified ClassLoader- See Also:
-
FACTORIES
If set, this map will contain Factory classes for creating hard to instantiate objects.- See Also:
-
-
Constructor Details
-
JsonReader
Deprecated.Deprecated - use JsonReader(ReadOptions)- Parameters:
maxDepth
- - maximum recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(ReadOptions) -
JsonReader
Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- - input stream to supply jsonmaxDepth
- - maximum recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- input stream
-
JsonReader
Deprecated.Deprecated - use JsonReader(ReadOptions) Use this constructor if you already have a JsonObject graph and want to parse it into Java objects by calling jsonReader.jsonObjectsToJava(rootJsonObject) after constructing the JsonReader.- Parameters:
optionalArgs
- Map of optional arguments for the JsonReader.maxDepth
- Maximum parsing depth.
-
JsonReader
Deprecated.Deprecated - use JsonReader(ReadOptions) Use this constructor if you already have a JsonObject graph and want to parse it into Java objects by calling jsonReader.jsonObjectsToJava(rootJsonObject) after constructing the JsonReader.- Parameters:
optionalArgs
- Map of optional arguments for the JsonReader.
-
JsonReader
Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- - json StringuseMaps
- - return data in map of mapsmaxDepth
- - maximum recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- - json StringuseMaps
- - return data in map of maps
-
JsonReader
@Deprecated public JsonReader(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- - json StringoptionalArgs
- - old way of passing reader argumentsmaxDepth
- - max recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(InputStream, ReadOptions)- Parameters:
inputStream
- - json StringoptionalArgs
- - old way of passing reader arguments
-
JsonReader
Deprecated.Deprecated - use JsonReader(String, ReadOptions)- Parameters:
json
- - json StringoptionalArgs
- - old way of passing reader argumentsmaxDepth
- - max recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(String, ReadOptions)- Parameters:
json
- - json StringoptionalArgs
- - old way of passing reader arguments
-
JsonReader
Deprecated.Deprecated - use JsonReader(byte[], ReadOptions)- Parameters:
inp
- - byte array with json data in it.optionalArgs
- - deprecated options.maxDepth
- - max recursion depth
-
JsonReader
Deprecated.Deprecated - use JsonReader(byte[], ReadOptions)- Parameters:
inp
- - byte array with json data in it.optionalArgs
- - deprecated options.
-
JsonReader
Creates a json reader using default read options- Parameters:
json
- - json String
-
JsonReader
Creates a json reader using custom read options- Parameters:
json
- json StringreadOptions
- Read Options
-
JsonReader
Creates a json reader using custom read options- Parameters:
bytes
- utf-8 encoded bytesreadOptions
- Read Options
-
JsonReader
Creates a json reader using custom read options- Parameters:
inp
- InputStream of utf-encoded jsonreadOptions
- Read Options
-
JsonReader
Use this constructor if you already have a JsonObject graph and want to parse it into Java objects by calling jsonReader.jsonObjectsToJava(rootJsonObject) after constructing the JsonReader.- Parameters:
options
- - read options
-
-
Method Details
-
assignInstantiator
@Deprecated public static void assignInstantiator(String className, JsonReader.ClassFactory factory) Deprecated.Deprecated - use ReadOptionsBuilder.assignInstantiator(String, ClassFactory)- Parameters:
className
- Class name to assign an ClassFactory tofactory
- ClassFactory that will create 'c' instances
-
assignInstantiator
Deprecated.Deprecated - use ReadOptionsBuilder.assignInstantiator(Class, ClassFactory)- Parameters:
c
- Class to assign on ClassFActoryfactory
- ClassFactory that wil create instance of that class.
-
addReader
Deprecated.use ReadOptionsBuilder.withCustomReader() to create any additional readers you'll need.Call this method to add a custom JSON reader to json-io. It will associate the Class 'c' to the reader you pass in. The readers are found with isAssignableFrom(). If this is too broad, causing too many classes to be associated to the custom reader, you can indicate that json-io should not use a custom reader for a particular class, by calling the addNotCustomReader() method.- Parameters:
c
- Class to assign a custom JSON reader toreader
- The JsonClassReader which will read the custom JSON format of 'c'
-
addNotCustomReader
Deprecated.use ReadOptionsBuilder.withNonCustomizableClass()Force json-io to use it's internal generic approach to writing the passed in class, even if a Custom JSON reader is specified for its parent class.- Parameters:
c
- Class to which to force no custom JSON reading to occur.
-
toObjects
Convert the passed in JSON string into a Java object graph.- Parameters:
input
- Input stream of UTF-8 json string dataoptions
- Read options- Returns:
- Java object graph matching JSON input
-
toObjects
Convert the passed in JSON string into a Java object graph.- Parameters:
bytes
- UTF-8 byte arrayoptions
- Read options- Returns:
- Java object graph matching JSON input
-
toObjects
Convert the passed in JSON string into a Java object graph.- Parameters:
jsonString
- String JSON inputoptions
- Read options- Returns:
- Java object graph matching JSON input
-
toObjects
Convert the passed in JSON string into a Java object graph. Uses the default read options- Parameters:
json
- String JSON input- Returns:
- Java object graph matching JSON input
-
jsonToJava
Deprecated.Convert the passed in JSON string into a Java object graph.- Parameters:
json
- String JSON input- Returns:
- Java object graph matching JSON input
-
jsonToJava
@Deprecated public static Object jsonToJava(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Convert the passed in JSON string into a Java object graph.- Parameters:
json
- String JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.maxDepth
- Maximum parsing depth.- Returns:
- Java object graph matching JSON input
-
jsonToJava
Deprecated.Convert the passed in JSON string into a Java object graph.- Parameters:
json
- String JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.- Returns:
- Java object graph matching JSON input
-
jsonToJava
@Deprecated public static <T> T jsonToJava(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Convert the passed in JSON string into a Java object graph.- Parameters:
inputStream
- InputStream containing JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.maxDepth
- Maximum parsing depth.- Returns:
- Java object graph matching JSON input
-
jsonToJava
@Deprecated public static <T> T jsonToJava(InputStream inputStream, Map<String, Object> optionalArgs) Deprecated.Convert the passed in JSON string into a Java object graph.- Parameters:
inputStream
- InputStream containing JSON inputoptionalArgs
- Map of optional parameters to control parsing. See readme file for details.- Returns:
- Java object graph matching JSON input
-
jsonToMaps
Deprecated.Deprecated = use toMaps(String, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(String json, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
json
- String of JSON contentmaxDepth
- Maximum parsing depth.- Returns:
- Map representing JSON content. Each object is represented by a Map.
-
jsonToMaps
Deprecated.Deprecated = use toMaps(String, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(String json, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
json
- String of JSON content- Returns:
- Map representing JSON content. Each object is represented by a Map.
-
jsonToMaps
@Deprecated public static Map jsonToMaps(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Deprecated = use toMaps(String, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(String json, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
json
- String of JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for details on these options.maxDepth
- Maximum parsing depth.- Returns:
- Map where each Map representa an object in the JSON input.
-
jsonToMaps
Deprecated.Deprecated = use toMaps(String, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(String json, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
json
- String of JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for details on these options.- Returns:
- Map where each Map representa an object in the JSON input.
-
jsonToMaps
@Deprecated public static Map jsonToMaps(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated.Deprecated = use toMaps(InputStream, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(inputStream, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
inputStream
- containing JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for details on these options.maxDepth
- Maximum parsing depth.- Returns:
- Map containing the content from the JSON input. Each Map represents an object from the input.
-
jsonToMaps
Deprecated.Deprecated = use toMaps(InputStream, ReadOptions); Map args = ["USE_MAPS": true] Use JsonReader.jsonToJava(inputStream, args) Note that the return type will match the JSON type (array, object, string, long, boolean, or null). No longer recommended: Use jsonToJava with USE_MAPS:true- Parameters:
inputStream
- containing JSON contentoptionalArgs
- Map of optional arguments to control customization. See readme file for details on these options.- Returns:
- Map containing the content from the JSON input. Each Map represents an object from the input.
-
toMaps
Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).- Parameters:
json
- json string- Returns:
- Map containing the content from the JSON input. Each Map represents an object from the input.
-
toMaps
Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).- Parameters:
json
- json stringreadOptions
- options to use when reading- Returns:
- Map containing the content from the JSON input. Each Map represents an object from the input.
-
toMaps
Note that the return type will match one of these JSON types (array, Map, string, long, boolean, or null).- Parameters:
inputStream
- bytes representing UTF-8 stringreadOptions
- options to use when reading- Returns:
- Map containing the content from the JSON input. Each Map represents an object from the input.
-
getReader
-
readObject
Read JSON input from the stream that was set up in the constructor, turning it into Java Maps (JsonObject's). Then, if requested, the JsonObjects can be converted into Java instances.- Returns:
- Java Object graph constructed from InputStream supplying JSON serialized content.
-
jsonObjectsToJava
Deprecated.Convert a root JsonObject that represents parsed JSON, into an actual Java object.- Parameters:
root
- JsonObject instance that was the root object from the JSON input that was parsed in an earlier call to JsonReader.- Returns:
- a typed Java instance that was serialized into JSON.
-
useMaps
protected boolean useMaps() -
convertParsedMapsToJava
This method converts a root Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance. The root map came from using the JsonReader to parse a JSON graph (using the API that puts the graph into Maps, not the typed representation).- Parameters:
root
- JsonObject instance that was the root object from thehint
- When you know the type you will be returning. JSON input that was parsed in an earlier call to JsonReader.- Returns:
- a typed Java instance that was serialized into JSON.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-