Class JSONSerializer


  • public final class JSONSerializer
    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.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.util.Comparator<java.lang.Object> SET_COMPARATOR
      Comparator for set elements, to sort them into some sort of consistent order, so that JSON ordering is deterministic.
    • Constructor Summary

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

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void assignObjectIds​(java.lang.Object jsonVal, java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal, ClassFieldCache classFieldCache, java.util.Map<ReferenceEqualityKey<JSONReference>,​java.lang.CharSequence> jsonReferenceToId, java.util.concurrent.atomic.AtomicInteger objId, boolean onlySerializePublicFields)
      Create a unique id for each referenced JSON object.
      private static void convertVals​(java.lang.Object[] convertedVals, java.util.Set<ReferenceEqualityKey<java.lang.Object>> visitedOnPath, java.util.Set<ReferenceEqualityKey<java.lang.Object>> standardObjectVisited, ClassFieldCache classFieldCache, java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal, boolean onlySerializePublicFields)
      Take an array of object values, and recursively convert them (in place) into JSON values.
      (package private) static void jsonValToJSONString​(java.lang.Object jsonVal, java.util.Map<ReferenceEqualityKey<JSONReference>,​java.lang.CharSequence> jsonReferenceToId, boolean includeNullValuedFields, int depth, int indentWidth, java.lang.StringBuilder buf)
      Serialize a JSON object, array, or value.
      static java.lang.String serializeFromField​(java.lang.Object containingObject, java.lang.String fieldName, int indentWidth, boolean onlySerializePublicFields)
      Recursively serialize the named field of an object, skipping transient and final fields.
      static java.lang.String serializeFromField​(java.lang.Object containingObject, java.lang.String fieldName, int indentWidth, boolean onlySerializePublicFields, ClassFieldCache classFieldCache)
      Recursively serialize the named field of an object, skipping transient and final fields.
      static java.lang.String serializeFromField​(java.lang.Object containingObject, java.lang.String fieldName, int indentWidth, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils)
      Recursively serialize the named field of an object, skipping transient and final fields.
      static java.lang.String serializeObject​(java.lang.Object obj)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
      static java.lang.String serializeObject​(java.lang.Object obj, int indentWidth, boolean onlySerializePublicFields)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
      static java.lang.String serializeObject​(java.lang.Object obj, int indentWidth, boolean onlySerializePublicFields, ClassFieldCache classFieldCache)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
      static java.lang.String serializeObject​(java.lang.Object obj, int indentWidth, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
      private static java.lang.Object toJSONGraph​(java.lang.Object obj, java.util.Set<ReferenceEqualityKey<java.lang.Object>> visitedOnPath, java.util.Set<ReferenceEqualityKey<java.lang.Object>> standardObjectVisited, ClassFieldCache classFieldCache, java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal, boolean onlySerializePublicFields)
      Turn an object graph into a graph of JSON objects, arrays, and values.
      • Methods inherited from class java.lang.Object

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

      • SET_COMPARATOR

        private static final java.util.Comparator<java.lang.Object> SET_COMPARATOR
        Comparator for set elements, to sort them into some sort of consistent order, so that JSON ordering is deterministic.
    • Constructor Detail

      • JSONSerializer

        private JSONSerializer()
        Constructor.
    • Method Detail

      • assignObjectIds

        private static void assignObjectIds​(java.lang.Object jsonVal,
                                            java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal,
                                            ClassFieldCache classFieldCache,
                                            java.util.Map<ReferenceEqualityKey<JSONReference>,​java.lang.CharSequence> jsonReferenceToId,
                                            java.util.concurrent.atomic.AtomicInteger objId,
                                            boolean onlySerializePublicFields)
        Create a unique id for each referenced JSON object.
        Parameters:
        jsonVal - the json val
        objToJSONVal - a map from obj to JSON val
        classFieldCache - the class field cache
        jsonReferenceToId - a map from json reference to id
        objId - the object id
        onlySerializePublicFields - whether to only serialize public fields
      • convertVals

        private static void convertVals​(java.lang.Object[] convertedVals,
                                        java.util.Set<ReferenceEqualityKey<java.lang.Object>> visitedOnPath,
                                        java.util.Set<ReferenceEqualityKey<java.lang.Object>> standardObjectVisited,
                                        ClassFieldCache classFieldCache,
                                        java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal,
                                        boolean onlySerializePublicFields)
        Take an array of object values, and recursively convert them (in place) into JSON values.
        Parameters:
        convertedVals - the converted vals
        visitedOnPath - visited nodes
        standardObjectVisited - visited standard objects
        classFieldCache - the class field cache
        objToJSONVal - a map from obj to JSON val
        onlySerializePublicFields - whether to only serialize public fields
      • toJSONGraph

        private static java.lang.Object toJSONGraph​(java.lang.Object obj,
                                                    java.util.Set<ReferenceEqualityKey<java.lang.Object>> visitedOnPath,
                                                    java.util.Set<ReferenceEqualityKey<java.lang.Object>> standardObjectVisited,
                                                    ClassFieldCache classFieldCache,
                                                    java.util.Map<ReferenceEqualityKey<java.lang.Object>,​JSONObject> objToJSONVal,
                                                    boolean onlySerializePublicFields)
        Turn an object graph into a graph of JSON objects, arrays, and values.
        Parameters:
        obj - the obj
        visitedOnPath - visited nodes
        standardObjectVisited - standard objects visited
        classFieldCache - the class field cache
        objToJSONVal - a map from obj to json val
        onlySerializePublicFields - whether to only serialize public fields
        Returns:
        the object
      • jsonValToJSONString

        static void jsonValToJSONString​(java.lang.Object jsonVal,
                                        java.util.Map<ReferenceEqualityKey<JSONReference>,​java.lang.CharSequence> jsonReferenceToId,
                                        boolean includeNullValuedFields,
                                        int depth,
                                        int indentWidth,
                                        java.lang.StringBuilder buf)
        Serialize a JSON object, array, or value.
        Parameters:
        jsonVal - the json val
        jsonReferenceToId - a map from json reference to id
        includeNullValuedFields - the include null valued fields
        depth - the depth
        indentWidth - the indent width
        buf - the buf
      • serializeObject

        public static java.lang.String serializeObject​(java.lang.Object obj,
                                                       int indentWidth,
                                                       boolean onlySerializePublicFields,
                                                       ClassFieldCache classFieldCache)
        Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
        Parameters:
        obj - The root object of the object graph to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        classFieldCache - The class field cache. Reusing this cache will increase the speed if many JSON documents of the same type need to be produced.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeObject

        public static java.lang.String serializeObject​(java.lang.Object obj,
                                                       int indentWidth,
                                                       boolean onlySerializePublicFields,
                                                       ReflectionUtils reflectionUtils)
        Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
        Parameters:
        obj - The root object of the object graph to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeObject

        public static java.lang.String serializeObject​(java.lang.Object obj,
                                                       int indentWidth,
                                                       boolean onlySerializePublicFields)
        Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
        Parameters:
        obj - The root object of the object graph to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeObject

        public static java.lang.String serializeObject​(java.lang.Object obj)
        Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
        Parameters:
        obj - The root object of the object graph to serialize.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeFromField

        public static java.lang.String serializeFromField​(java.lang.Object containingObject,
                                                          java.lang.String fieldName,
                                                          int indentWidth,
                                                          boolean onlySerializePublicFields,
                                                          ClassFieldCache classFieldCache)
        Recursively serialize the named field of an object, skipping transient and final fields.
        Parameters:
        containingObject - The object containing the field value to serialize.
        fieldName - The name of the field to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        classFieldCache - The class field cache. Reusing this cache will increase the speed if many JSON documents of the same type need to be produced.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeFromField

        public static java.lang.String serializeFromField​(java.lang.Object containingObject,
                                                          java.lang.String fieldName,
                                                          int indentWidth,
                                                          boolean onlySerializePublicFields,
                                                          ReflectionUtils reflectionUtils)
        Recursively serialize the named field of an object, skipping transient and final fields.
        Parameters:
        containingObject - The object containing the field value to serialize.
        fieldName - The name of the field to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        reflectionUtils - The reflection driver.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.
      • serializeFromField

        public static java.lang.String serializeFromField​(java.lang.Object containingObject,
                                                          java.lang.String fieldName,
                                                          int indentWidth,
                                                          boolean onlySerializePublicFields)
        Recursively serialize the named field of an object, skipping transient and final fields.
        Parameters:
        containingObject - The object containing the field value to serialize.
        fieldName - The name of the field to serialize.
        indentWidth - If indentWidth == 0, no prettyprinting indentation is performed, otherwise this specifies the number of spaces to indent each level of JSON.
        onlySerializePublicFields - If true, only serialize public fields.
        Returns:
        The object graph in JSON form.
        Throws:
        java.lang.IllegalArgumentException - If anything goes wrong during serialization.