Package nonapi.io.github.classgraph.json
Class JSONUtils
- java.lang.Object
-
- nonapi.io.github.classgraph.json.JSONUtils
-
public final class JSONUtils extends java.lang.Object
Utils for Java serialization and deserialization.
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static java.lang.String
ID_KEY
JSON object key name for objects that are linked to from more than one object.(package private) static java.lang.String
ID_PREFIX
JSON object reference id prefix.(package private) static java.lang.String
ID_SUFFIX
JSON object reference id suffix.private static java.lang.String[]
INDENT_LEVELS
Lookup table for fast indenting.private static java.lang.reflect.Method
isAccessibleMethod
private static java.lang.String[]
JSON_CHAR_REPLACEMENTS
JSON character-to-string escaping replacements -- see http://www.json.org/ under "string".private static java.lang.reflect.Method
setAccessibleMethod
private static java.lang.reflect.Method
trySetAccessibleMethod
-
Constructor Summary
Constructors Modifier Constructor Description private
JSONUtils()
Constructor.
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.String
escapeJSONString(java.lang.String unsafeStr)
Escape a string to be surrounded in double quotes in JSON.(package private) static void
escapeJSONString(java.lang.String unsafeStr, java.lang.StringBuilder buf)
Escape a string to be surrounded in double quotes in JSON.(package private) static boolean
fieldIsSerializable(java.lang.reflect.Field field, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils)
Check if a field is serializable.(package private) static java.lang.Object
getFieldValue(java.lang.Object containingObj, java.lang.reflect.Field field)
Get a field value, appropriately handling primitive-typed fields.(package private) static java.lang.Class<?>
getRawType(java.lang.reflect.Type type)
Get the raw type from a Type.(package private) static void
indent(int depth, int indentWidth, java.lang.StringBuilder buf)
Indent (depth * indentWidth) spaces.private static boolean
isAccessible(java.lang.reflect.AccessibleObject obj)
(package private) static boolean
isBasicValueType(java.lang.Class<?> cls)
Return true for classes that can be equal to a basic value type (types that can be converted directly to and from string representation).(package private) static boolean
isBasicValueType(java.lang.Object obj)
Return true for objects that can be converted directly to and from string representation.(package private) static boolean
isBasicValueType(java.lang.reflect.Type type)
Return true for types that can be converted directly to and from string representation.(package private) static boolean
isCollectionOrArray(java.lang.Object obj)
Return true for objects that are collections or arrays (i.e.static boolean
makeAccessible(java.lang.reflect.AccessibleObject obj, ReflectionUtils reflectionUtils)
private static boolean
tryMakeAccessible(java.lang.reflect.AccessibleObject obj)
-
-
-
Field Detail
-
isAccessibleMethod
private static java.lang.reflect.Method isAccessibleMethod
-
setAccessibleMethod
private static java.lang.reflect.Method setAccessibleMethod
-
trySetAccessibleMethod
private static java.lang.reflect.Method trySetAccessibleMethod
-
ID_KEY
static final java.lang.String ID_KEY
JSON object key name for objects that are linked to from more than one object. Key name is only used if the class that a JSON object was serialized from does not have its own id field annotated withId
.- See Also:
- Constant Field Values
-
ID_PREFIX
static final java.lang.String ID_PREFIX
JSON object reference id prefix.- See Also:
- Constant Field Values
-
ID_SUFFIX
static final java.lang.String ID_SUFFIX
JSON object reference id suffix.- See Also:
- Constant Field Values
-
JSON_CHAR_REPLACEMENTS
private static final java.lang.String[] JSON_CHAR_REPLACEMENTS
JSON character-to-string escaping replacements -- see http://www.json.org/ under "string".
-
INDENT_LEVELS
private static final java.lang.String[] INDENT_LEVELS
Lookup table for fast indenting.
-
-
Method Detail
-
isAccessible
private static boolean isAccessible(java.lang.reflect.AccessibleObject obj)
-
tryMakeAccessible
private static boolean tryMakeAccessible(java.lang.reflect.AccessibleObject obj)
-
makeAccessible
public static boolean makeAccessible(java.lang.reflect.AccessibleObject obj, ReflectionUtils reflectionUtils)
-
escapeJSONString
static void escapeJSONString(java.lang.String unsafeStr, java.lang.StringBuilder buf)
Escape a string to be surrounded in double quotes in JSON.- Parameters:
unsafeStr
- the unsafe strbuf
- the buf
-
escapeJSONString
public static java.lang.String escapeJSONString(java.lang.String unsafeStr)
Escape a string to be surrounded in double quotes in JSON.- Parameters:
unsafeStr
- The string to escape.- Returns:
- The escaped string.
-
indent
static void indent(int depth, int indentWidth, java.lang.StringBuilder buf)
Indent (depth * indentWidth) spaces.- Parameters:
depth
- the depthindentWidth
- the indent widthbuf
- the buf
-
getFieldValue
static java.lang.Object getFieldValue(java.lang.Object containingObj, java.lang.reflect.Field field) throws java.lang.IllegalArgumentException, java.lang.IllegalAccessException
Get a field value, appropriately handling primitive-typed fields.- Parameters:
containingObj
- the containing objectfield
- the field- Returns:
- the field value
- Throws:
java.lang.IllegalArgumentException
- if the specified object is not an instance of the class or interface declaring the underlying fieldjava.lang.IllegalAccessException
- if the field cannot be read
-
isBasicValueType
static boolean isBasicValueType(java.lang.Class<?> cls)
Return true for classes that can be equal to a basic value type (types that can be converted directly to and from string representation).- Parameters:
cls
- the class- Returns:
- true, if the class is a basic value type
-
isBasicValueType
static boolean isBasicValueType(java.lang.reflect.Type type)
Return true for types that can be converted directly to and from string representation.- Parameters:
type
- the type- Returns:
- true, if the type is a basic value type
-
isBasicValueType
static boolean isBasicValueType(java.lang.Object obj)
Return true for objects that can be converted directly to and from string representation.- Parameters:
obj
- the object- Returns:
- true, if the object is null or of basic value type
-
isCollectionOrArray
static boolean isCollectionOrArray(java.lang.Object obj)
Return true for objects that are collections or arrays (i.e. objects that are convertible to a JSON array).- Parameters:
obj
- the object- Returns:
- true, if the object is a collection or array
-
getRawType
static java.lang.Class<?> getRawType(java.lang.reflect.Type type)
Get the raw type from a Type.- Parameters:
type
- the type- Returns:
- the raw type
- Throws:
java.lang.IllegalArgumentException
- if passed a TypeVariable or anything other than aClass<?>
reference orParameterizedType
.
-
fieldIsSerializable
static boolean fieldIsSerializable(java.lang.reflect.Field field, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils)
Check if a field is serializable. Don't serialize transient, final, synthetic, or inaccessible fields.N.B. Tries to set field to accessible, which will require an "opens" declarations from modules that want to allow this introspection.
- Parameters:
field
- the fieldonlySerializePublicFields
- if true, only serialize public fields- Returns:
- true if the field is serializable
-
-