Class JsonReader

java.lang.Object
com.cedarsoftware.io.JsonReader
All Implemented Interfaces:
Closeable, AutoCloseable

public class JsonReader extends Object implements Closeable
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:
  • 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 call readObject(). 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 call readObject(). 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.
  • Constructor Details

    • JsonReader

      public JsonReader(InputStream input, ReadOptions readOptions)
      Creates a json reader using custom read options
      Parameters:
      input - InputStream of utf-encoded json
      readOptions - Read Options to turn on/off various feature options, or supply additional ClassFactory data, etc. If null, readOptions will use all defaults.
    • JsonReader

      public JsonReader(InputStream inputStream, ReadOptions readOptions, ReferenceTracker references)
    • JsonReader

      public JsonReader(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.
      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

      protected com.cedarsoftware.util.FastReader getReader(InputStream inputStream)
      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

      public <T> T readObject(Class<T> rootType)
    • toJavaObjects

      protected <T> T toJavaObjects(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. 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.
    • getResolver

      public Resolver getResolver()
      Returns:
      a copy of the Resolver being used. The Resolver is a super class of ObjectResolver or MapResolver. The ObjectResolver is used to convert JSON maps (JsonObject) to Java objects. A MapResolver is used to convert clean-up the value-side of the map only. The MapResolver uses the Class associated to the JsonObject to coerce primitive fields in the Map's values to their correct data types. For example, if a Long was read in as a String from the JSON, it will be converted back to a Long (inside the Map) because the associated field (matched by String key name in Map) to the Java class 's Field, and converts the raw JSON primitive value to the field type (long/Long).
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable