Class JsonReader

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

public class JsonReader extends Object implements Closeable, ReaderContext
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:
    • jsonToJava

      @Deprecated public static <T> T jsonToJava(String json)
      Deprecated.
      "Jumper" APIs to support past API usage.
    • jsonToJava

      @Deprecated public static <T> T jsonToJava(String json, Map<String,Object> optionalArgs)
      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 public Object jsonObjectsToJava(JsonObject root)
      Deprecated.
    • jsonToMaps

      @Deprecated public static Map jsonToMaps(String json)
      Deprecated.
    • jsonToMaps

      @Deprecated public static Map jsonToMaps(String json, int maxDepth)
      Deprecated.
    • jsonToMaps

      @Deprecated public static Map jsonToMaps(String json, Map<String,Object> optionalArgs, int maxDepth)
      Deprecated.
    • readObject

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

      public ClassLoader getClassLoader()
      Returns:
      ClassLoader to be used by Custom Writers
    • getReferences

      public ReferenceTracker getReferences()
      Specified by:
      getReferences in interface ReaderContext
    • reentrantConvertJsonValueToJava

      public <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. 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 interface ReaderContext
      Parameters:
      rootObj - JsonObject instance that was the rootObj object from the
      root - 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

      public 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.
      Specified by:
      traverseFields in interface ReaderContext
      Parameters:
      stack - Stack (Deque) used for graph traversal.
      jsonObj - a Map-of-Map representation of the current object being examined (containing all fields).
    • convertJsonValueToJava

      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. 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 interface AutoCloseable
      Specified by:
      close in interface Closeable