Package com.cedarsoftware.util.io
Class JsonReader
java.lang.Object
com.cedarsoftware.util.io.JsonReader
- All Implemented Interfaces:
ReaderContext
,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 'Serializable'
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
License
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
Modifier and TypeClassDescriptionstatic interface
Subclass this interface and create a class that will return a new instance of the passed in Class (c).static interface
Implement this interface to add a custom JSON reader.static interface
Used to react to fields missing when reading an object. -
Constructor Summary
ConstructorDescriptionJsonReader
(ReadOptions 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.JsonReader
(InputStream input, ReadOptions readOptions) Creates a json reader using custom read optionsJsonReader
(InputStream inputStream, ReadOptions readOptions, ReferenceTracker references) -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
protected <T> T
convertJsonValueToJava
(JsonObject rootObj, Class<T> root) This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance.protected com.cedarsoftware.util.FastReader
getReader
(InputStream inputStream) Allow others to try potentially faster Readers.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
(String json) Deprecated.static Map
jsonToMaps
(String json, int maxDepth) Deprecated.static Map
jsonToMaps
(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated.<T> T
readObject
(Class<T> rootType) <T> T
reentrantConvertJsonValueToJava
(JsonObject rootObj, Class<T> root) This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance.void
traverseFields
(Deque<JsonObject> stack, JsonObject jsonObj) Walk the Java object fields and copy them from the JSON object to the Java object, performing any necessary conversions on primitives, or deep traversals for field assignments to other objects, arrays, Collections, or Maps.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface com.cedarsoftware.util.io.ReaderContext
getConverter, getReadOptions
-
Constructor Details
-
JsonReader
Creates a json reader using custom read options- Parameters:
input
- InputStream of utf-encoded jsonreadOptions
- Read Options to turn on/off various feature options, or supply additional ClassFactory data, etc. If null, readOptions will use all defaults.
-
JsonReader
-
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:
readOptions
- Read Options to turn on/off various feature options, or supply additional ClassFactory data, etc. If null, readOptions will use all defaults.
-
-
Method Details
-
getReader
Allow others to try potentially faster Readers.- Parameters:
inputStream
- InputStream that will be offering JSON.- Returns:
-
jsonToJava
Deprecated."Jumper" APIs to support past API usage. -
jsonToJava
Deprecated. -
jsonToJava
@Deprecated public static Object jsonToJava(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated. -
jsonToJava
@Deprecated public static <T> T jsonToJava(InputStream inputStream, Map<String, Object> optionalArgs) Deprecated. -
jsonToJava
@Deprecated public static <T> T jsonToJava(InputStream inputStream, Map<String, Object> optionalArgs, int maxDepth) Deprecated. -
jsonObjectsToJava
Deprecated. -
jsonToMaps
Deprecated. -
jsonToMaps
Deprecated. -
jsonToMaps
@Deprecated public static Map jsonToMaps(String json, Map<String, Object> optionalArgs, int maxDepth) Deprecated. -
readObject
-
getClassLoader
- Returns:
- ClassLoader to be used by Custom Writers
-
getReferences
- Specified by:
getReferences
in interfaceReaderContext
-
reentrantConvertJsonValueToJava
This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance. The rootObj map came from using the JsonReader to parse a JSON graph (using the API that puts the graph into Maps, not the typed representation).- Specified by:
reentrantConvertJsonValueToJava
in interfaceReaderContext
- Parameters:
rootObj
- JsonObject instance that was the rootObj object from theroot
- When you know the type you will be returning. Can be null (effectively Map.class) JSON input that was parsed in an earlier call to JsonReader.- Returns:
- a typed Java instance that was serialized into JSON.
-
traverseFields
Walk the Java object fields and copy them from the JSON object to the Java object, performing any necessary conversions on primitives, or deep traversals for field assignments to other objects, arrays, Collections, or Maps.- Specified by:
traverseFields
in interfaceReaderContext
- Parameters:
stack
- Stack (Deque) used for graph traversal.jsonObj
- a Map-of-Map representation of the current object being examined (containing all fields).
-
convertJsonValueToJava
This method converts a rootObj Map, (which contains nested Maps and so forth representing a Java Object graph), to a Java object instance. The rootObj 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:
rootObj
- JsonObject instance that was the rootObj object from the 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
-