Class JSONDeserializer


  • public class JSONDeserializer
    extends java.lang.Object
    Fast, lightweight Java object to JSON serializer, and JSON to Java object deserializer. Handles cycles in the object graph by inserting reference ids.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      private static class  JSONDeserializer.ObjectInstantiation
      Used to hold object instantiations temporarily before their fields can be populated, so that object references can be resolved in the same order during deserialization as they were created during serialization.
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private JSONDeserializer()
      Constructor.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static <T> T deserializeObject​(java.lang.Class<T> expectedType, java.lang.String json)
      Deserialize JSON to a new object graph, with the root object of the specified expected type.
      private static <T> T deserializeObject​(java.lang.Class<T> expectedType, java.lang.String json, ClassFieldCache classFieldCache)
      Deserialize JSON to a new object graph, with the root object of the specified expected type, using or reusing the given type cache.
      static <T> T deserializeObject​(java.lang.Class<T> expectedType, java.lang.String json, ReflectionUtils reflectionUtils)
      Deserialize JSON to a new object graph, with the root object of the specified expected type.
      static void deserializeToField​(java.lang.Object containingObject, java.lang.String fieldName, java.lang.String json)
      Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object.
      static void deserializeToField​(java.lang.Object containingObject, java.lang.String fieldName, java.lang.String json, ClassFieldCache classFieldCache)
      Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object.
      static void deserializeToField​(java.lang.Object containingObject, java.lang.String fieldName, java.lang.String json, ReflectionUtils reflectionUtils)
      Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object.
      private static java.util.Map<java.lang.CharSequence,​java.lang.Object> getInitialIdToObjectMap​(java.lang.Object objectInstance, java.lang.Object parsedJSON)
      Set up the initial mapping from id to object, by adding the id of the toplevel object, if it has an id field in JSON.
      private static java.lang.Object jsonBasicValueToObject​(java.lang.Object jsonVal, java.lang.reflect.Type expectedType, boolean convertStringToNumber)
      Deserialize a JSON basic value (String, Integer, Long, or Double), conforming it to the expected type (Character, Short, etc.).
      private static void populateObjectFromJsonObject​(java.lang.Object objectInstance, java.lang.reflect.Type objectResolvedType, java.lang.Object jsonVal, ClassFieldCache classFieldCache, java.util.Map<java.lang.CharSequence,​java.lang.Object> idToObjectInstance, java.util.List<java.lang.Runnable> collectionElementAdders)
      Populate object from json object.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JSONDeserializer

        private JSONDeserializer()
        Constructor.
    • Method Detail

      • jsonBasicValueToObject

        private static java.lang.Object jsonBasicValueToObject​(java.lang.Object jsonVal,
                                                               java.lang.reflect.Type expectedType,
                                                               boolean convertStringToNumber)
        Deserialize a JSON basic value (String, Integer, Long, or Double), conforming it to the expected type (Character, Short, etc.).
        Parameters:
        jsonVal - the json val
        expectedType - the expected type
        convertStringToNumber - if true, convert strings to numbers
        Returns:
        the object
      • populateObjectFromJsonObject

        private static void populateObjectFromJsonObject​(java.lang.Object objectInstance,
                                                         java.lang.reflect.Type objectResolvedType,
                                                         java.lang.Object jsonVal,
                                                         ClassFieldCache classFieldCache,
                                                         java.util.Map<java.lang.CharSequence,​java.lang.Object> idToObjectInstance,
                                                         java.util.List<java.lang.Runnable> collectionElementAdders)
        Populate object from json object.
        Parameters:
        objectInstance - the object instance
        objectResolvedType - the object resolved type
        jsonVal - the json val
        classFieldCache - the class field cache
        idToObjectInstance - a map from id to object instance
        collectionElementAdders - the collection element adders
      • getInitialIdToObjectMap

        private static java.util.Map<java.lang.CharSequence,​java.lang.Object> getInitialIdToObjectMap​(java.lang.Object objectInstance,
                                                                                                            java.lang.Object parsedJSON)
        Set up the initial mapping from id to object, by adding the id of the toplevel object, if it has an id field in JSON.
        Parameters:
        objectInstance - the object instance
        parsedJSON - the parsed JSON
        Returns:
        the initial id to object map
      • deserializeObject

        private static <T> T deserializeObject​(java.lang.Class<T> expectedType,
                                               java.lang.String json,
                                               ClassFieldCache classFieldCache)
                                        throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type, using or reusing the given type cache. Does not work for generic types, since it is not possible to obtain the generic type of a Class reference.
        Type Parameters:
        T - the expected type
        Parameters:
        expectedType - The type that the JSON should conform to.
        json - the JSON string to deserialize.
        classFieldCache - The class field cache. Reusing this cache will increase the speed if many JSON documents of the same type need to be parsed.
        Returns:
        The object graph after deserialization.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.
      • deserializeObject

        public static <T> T deserializeObject​(java.lang.Class<T> expectedType,
                                              java.lang.String json,
                                              ReflectionUtils reflectionUtils)
                                       throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type. Does not work for generic types, since it is not possible to obtain the generic type of a Class reference.
        Type Parameters:
        T - The type that the JSON should conform to.
        Parameters:
        expectedType - The class reference for the type that the JSON should conform to.
        json - the JSON string to deserialize.
        Returns:
        The object graph after deserialization.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.
      • deserializeObject

        public static <T> T deserializeObject​(java.lang.Class<T> expectedType,
                                              java.lang.String json)
                                       throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type. Does not work for generic types, since it is not possible to obtain the generic type of a Class reference.
        Type Parameters:
        T - The type that the JSON should conform to.
        Parameters:
        expectedType - The class reference for the type that the JSON should conform to.
        json - the JSON string to deserialize.
        Returns:
        The object graph after deserialization.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.
      • deserializeToField

        public static void deserializeToField​(java.lang.Object containingObject,
                                              java.lang.String fieldName,
                                              java.lang.String json,
                                              ClassFieldCache classFieldCache)
                                       throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object. Works for generic types, since it is possible to obtain the generic type of a field.
        Parameters:
        containingObject - The object containing the named field to deserialize the object graph into.
        fieldName - The name of the field to set with the result.
        json - the JSON string to deserialize.
        classFieldCache - The class field cache. Reusing this cache will increase the speed if many JSON documents of the same type need to be parsed.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.
      • deserializeToField

        public static void deserializeToField​(java.lang.Object containingObject,
                                              java.lang.String fieldName,
                                              java.lang.String json,
                                              ReflectionUtils reflectionUtils)
                                       throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object. Works for generic types, since it is possible to obtain the generic type of a field.
        Parameters:
        containingObject - The object containing the named field to deserialize the object graph into.
        fieldName - The name of the field to set with the result.
        json - the JSON string to deserialize.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.
      • deserializeToField

        public static void deserializeToField​(java.lang.Object containingObject,
                                              java.lang.String fieldName,
                                              java.lang.String json)
                                       throws java.lang.IllegalArgumentException
        Deserialize JSON to a new object graph, with the root object of the specified expected type, and store the root object in the named field of the given containing object. Works for generic types, since it is possible to obtain the generic type of a field.
        Parameters:
        containingObject - The object containing the named field to deserialize the object graph into.
        fieldName - The name of the field to set with the result.
        json - the JSON string to deserialize.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during deserialization.