Class JSONSerializer


  • public class JSONSerializer
    extends Object
    Fast, lightweight Java object to JSON serializer, and JSON to Java object deserializer. Handles cycles in the object graph by inserting reference ids.
    • Constructor Summary

      Constructors 
      Constructor Description
      JSONSerializer()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static String serializeFromField​(Object containingObject, String fieldName, int indentWidth, boolean onlySerializePublicFields)
      Recursively serialize the named field of an object, skipping transient and final fields.
      static String serializeFromField​(Object containingObject, String fieldName, int indentWidth, boolean onlySerializePublicFields, io.github.classgraph.json.ClassFieldCache classFieldCache)
      Recursively serialize the named field of an object, skipping transient and final fields.
      static String serializeObject​(Object obj)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
      static String serializeObject​(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 String serializeObject​(Object obj, int indentWidth, boolean onlySerializePublicFields, io.github.classgraph.json.ClassFieldCache classFieldCache)
      Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.
    • Constructor Detail

      • JSONSerializer

        public JSONSerializer()
    • Method Detail

      • serializeObject

        public static String serializeObject​(Object obj,
                                             int indentWidth,
                                             boolean onlySerializePublicFields,
                                             io.github.classgraph.json.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:
        IllegalArgumentException - If anything goes wrong during serialization.
      • serializeObject

        public static String serializeObject​(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:
        IllegalArgumentException - If anything goes wrong during serialization.
      • serializeObject

        public static String serializeObject​(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:
        IllegalArgumentException - If anything goes wrong during serialization.
      • serializeFromField

        public static String serializeFromField​(Object containingObject,
                                                String fieldName,
                                                int indentWidth,
                                                boolean onlySerializePublicFields,
                                                io.github.classgraph.json.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:
        IllegalArgumentException - If anything goes wrong during serialization.
      • serializeFromField

        public static String serializeFromField​(Object containingObject,
                                                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:
        IllegalArgumentException - If anything goes wrong during serialization.