Class JsonReader
- All Implemented Interfaces:
Closeable,AutoCloseable
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
Nested ClassesModifier and TypeClassDescriptionstatic interfaceSubclass this interface and create a class that will return a new instance of the passed in Class (c).static interfaceImplement this interface to add a custom JSON reader.static interfaceUsed to react to fields missing when reading an object. -
Constructor Summary
ConstructorsConstructorDescriptionJsonReader(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 TypeMethodDescriptionvoidclose()protected com.cedarsoftware.util.FastReadergetReader(InputStream inputStream) Allow others to try potentially faster Readers.Returns the currentResolverinstance used for JSON deserialization.<T> TreadObject(Class<T> rootType) protected <T> TresolveObjects(JsonObject rootObj, Class<T> rootType) Deserializes a JSON object graph into a strongly-typed Java object instance.
-
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:
- FastReader wrapped around the passed in inputStream, translating from InputStream to InputStreamReader.
-
readObject
-
resolveObjects
Deserializes a JSON object graph into a strongly-typed Java object instance.This method processes a root
JsonObjectthat represents the serialized form of a Java object graph, which may include nestedMapinstances and other complex structures. It converts this JSON-based representation back into a typed Java object, optionally guided by the specifiedrootType.If the
rootTypeparameter isnull, the method attempts to infer the appropriate Java class from the@typeannotation within therootObj. In the absence of such type information, it defaults toObject.After the deserialization process, the method ensures that the resolver's state is cleaned up, maintaining the integrity of subsequent operations.
- Type Parameters:
T- the expected type of the Java object to be returned- Parameters:
rootObj- the rootJsonObjectrepresenting the serialized JSON object graph to be deserializedrootType- the desired Java type for the root object. Ifnull, the method will attempt to determine the type from the@typeannotation inrootObj- Returns:
- a Java object instance corresponding to the provided JSON graph, of type
T - Throws:
JsonIoException- if an error occurs during deserialization, such as type conversion issues or I/O errorsImplementation Notes:
- When
rootTypeisnull, the method prioritizes type inference from the@typeannotation withinrootObj. If no type information is available, it defaults toObject. - If
ReadOptions.isCloseStream()istrue, the method ensures that the associated stream is closed upon encountering an exception to prevent resource leaks. - Post-deserialization, the resolver's state is cleaned up to maintain consistency and prevent interference with subsequent deserialization operations.
- When
-
getResolver
Returns the currentResolverinstance used for JSON deserialization.The
Resolverserves as the superclass for bothObjectResolverandMapResolver, each handling specific aspects of converting JSON structures into Java objects.- ObjectResolver:
Responsible for converting JSON maps (represented by
JsonObject) into their corresponding Java object instances. It handles the instantiation of Java classes and populates their fields based on the JSON data. - MapResolver:
Focuses on refining the value side within a map. It utilizes the class information associated with a
JsonObjectto coerce primitive fields in the map's values to their correct data types. For example, if aLongis serialized as aStringin the JSON, theMapResolverwill convert it back to aLongwithin the map by matching the JSON key to the corresponding field in the Java class and transforming the raw JSON primitive value to the field's type (e.g.,long/Long).
This method is essential for scenarios where JSON data needs to be deserialized into Java objects, ensuring that both object instantiation and type coercion are handled appropriately.
- Returns:
- the
Resolvercurrently in use. ThisResolveris the superclass forObjectResolverandMapResolver, facilitating the conversion of JSON structures into Java objects and the coercion of map values to their correct data types.
- ObjectResolver:
-
close
public void close()- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable
-