Package nonapi.io.github.classgraph.json
Class JSONSerializer
java.lang.Object
nonapi.io.github.classgraph.json.JSONSerializer
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
FieldsModifier and TypeFieldDescriptionprivate static final Comparator
<Object> Comparator for set elements, to sort them into some sort of consistent order, so that JSON ordering is deterministic. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
assignObjectIds
(Object jsonVal, Map<ReferenceEqualityKey<Object>, JSONObject> objToJSONVal, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<JSONReference>, CharSequence> jsonReferenceToId, AtomicInteger objId, boolean onlySerializePublicFields) Create a unique id for each referenced JSON object.private static void
convertVals
(Object[] convertedVals, Set<ReferenceEqualityKey<Object>> visitedOnPath, Set<ReferenceEqualityKey<Object>> standardObjectVisited, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<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
(Object jsonVal, Map<ReferenceEqualityKey<JSONReference>, CharSequence> jsonReferenceToId, boolean includeNullValuedFields, int depth, int indentWidth, StringBuilder buf) Serialize a JSON object, array, or value.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, ClassFieldCache classFieldCache) Recursively serialize the named field of an object, skipping transient and final fields.static String
serializeFromField
(Object containingObject, String fieldName, int indentWidth, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils) 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, ClassFieldCache classFieldCache) 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, ReflectionUtils reflectionUtils) Recursively serialize an Object (or array, list, map or set of objects) to JSON, skipping transient and final fields.private static Object
toJSONGraph
(Object obj, Set<ReferenceEqualityKey<Object>> visitedOnPath, Set<ReferenceEqualityKey<Object>> standardObjectVisited, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<Object>, JSONObject> objToJSONVal, boolean onlySerializePublicFields) Turn an object graph into a graph of JSON objects, arrays, and values.
-
Field Details
-
SET_COMPARATOR
Comparator for set elements, to sort them into some sort of consistent order, so that JSON ordering is deterministic.
-
-
Constructor Details
-
JSONSerializer
private JSONSerializer()Constructor.
-
-
Method Details
-
assignObjectIds
private static void assignObjectIds(Object jsonVal, Map<ReferenceEqualityKey<Object>, JSONObject> objToJSONVal, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<JSONReference>, CharSequence> jsonReferenceToId, AtomicInteger objId, boolean onlySerializePublicFields) Create a unique id for each referenced JSON object.- Parameters:
jsonVal
- the json valobjToJSONVal
- a map from obj to JSON valclassFieldCache
- the class field cachejsonReferenceToId
- a map from json reference to idobjId
- the object idonlySerializePublicFields
- whether to only serialize public fields
-
convertVals
private static void convertVals(Object[] convertedVals, Set<ReferenceEqualityKey<Object>> visitedOnPath, Set<ReferenceEqualityKey<Object>> standardObjectVisited, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<Object>, JSONObject> objToJSONVal, boolean onlySerializePublicFields) Take an array of object values, and recursively convert them (in place) into JSON values.- Parameters:
convertedVals
- the converted valsvisitedOnPath
- visited nodesstandardObjectVisited
- visited standard objectsclassFieldCache
- the class field cacheobjToJSONVal
- a map from obj to JSON valonlySerializePublicFields
- whether to only serialize public fields
-
toJSONGraph
private static Object toJSONGraph(Object obj, Set<ReferenceEqualityKey<Object>> visitedOnPath, Set<ReferenceEqualityKey<Object>> standardObjectVisited, ClassFieldCache classFieldCache, Map<ReferenceEqualityKey<Object>, JSONObject> objToJSONVal, boolean onlySerializePublicFields) Turn an object graph into a graph of JSON objects, arrays, and values.- Parameters:
obj
- the objvisitedOnPath
- visited nodesstandardObjectVisited
- standard objects visitedclassFieldCache
- the class field cacheobjToJSONVal
- a map from obj to json valonlySerializePublicFields
- whether to only serialize public fields- Returns:
- the object
-
jsonValToJSONString
static void jsonValToJSONString(Object jsonVal, Map<ReferenceEqualityKey<JSONReference>, CharSequence> jsonReferenceToId, boolean includeNullValuedFields, int depth, int indentWidth, StringBuilder buf) Serialize a JSON object, array, or value.- Parameters:
jsonVal
- the json valjsonReferenceToId
- a map from json reference to idincludeNullValuedFields
- the include null valued fieldsdepth
- the depthindentWidth
- the indent widthbuf
- the buf
-
serializeObject
public static String serializeObject(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:
IllegalArgumentException
- If anything goes wrong during serialization.
-
serializeObject
public static String serializeObject(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:
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
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, 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, 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:
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.
-