Package nonapi.io.github.classgraph.json
Class JSONUtils
java.lang.Object
nonapi.io.github.classgraph.json.JSONUtils
Utils for Java serialization and deserialization.
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final String
JSON object key name for objects that are linked to from more than one object.(package private) static final String
JSON object reference id prefix.(package private) static final String
JSON object reference id suffix.private static final String[]
Lookup table for fast indenting.private static Method
private static final String[]
JSON character-to-string escaping replacements -- see http://www.json.org/ under "string".private static Method
private static Method
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic String
escapeJSONString
(String unsafeStr) Escape a string to be surrounded in double quotes in JSON.(package private) static void
escapeJSONString
(String unsafeStr, StringBuilder buf) Escape a string to be surrounded in double quotes in JSON.(package private) static boolean
fieldIsSerializable
(Field field, boolean onlySerializePublicFields, ReflectionUtils reflectionUtils) Check if a field is serializable.(package private) static Object
getFieldValue
(Object containingObj, Field field) Get a field value, appropriately handling primitive-typed fields.(package private) static Class
<?> getRawType
(Type type) Get the raw type from a Type.(package private) static void
indent
(int depth, int indentWidth, StringBuilder buf) Indent (depth * indentWidth) spaces.private static boolean
(package private) static boolean
isBasicValueType
(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
(Object obj) Return true for objects that can be converted directly to and from string representation.(package private) static boolean
isBasicValueType
(Type type) Return true for types that can be converted directly to and from string representation.(package private) static boolean
Return true for objects that are collections or arrays (i.e.static boolean
makeAccessible
(AccessibleObject obj, ReflectionUtils reflectionUtils) private static boolean
-
Field Details
-
isAccessibleMethod
-
setAccessibleMethod
-
trySetAccessibleMethod
-
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:
-
ID_PREFIX
JSON object reference id prefix.- See Also:
-
ID_SUFFIX
JSON object reference id suffix.- See Also:
-
JSON_CHAR_REPLACEMENTS
JSON character-to-string escaping replacements -- see http://www.json.org/ under "string". -
INDENT_LEVELS
Lookup table for fast indenting.
-
-
Constructor Details
-
JSONUtils
private JSONUtils()Constructor.
-
-
Method Details
-
isAccessible
-
tryMakeAccessible
-
makeAccessible
-
escapeJSONString
Escape a string to be surrounded in double quotes in JSON.- Parameters:
unsafeStr
- the unsafe strbuf
- the buf
-
escapeJSONString
Escape a string to be surrounded in double quotes in JSON.- Parameters:
unsafeStr
- The string to escape.- Returns:
- The escaped string.
-
indent
Indent (depth * indentWidth) spaces.- Parameters:
depth
- the depthindentWidth
- the indent widthbuf
- the buf
-
getFieldValue
static Object getFieldValue(Object containingObj, Field field) throws IllegalArgumentException, IllegalAccessException Get a field value, appropriately handling primitive-typed fields.- Parameters:
containingObj
- the containing objectfield
- the field- Returns:
- the field value
- Throws:
IllegalArgumentException
- if the specified object is not an instance of the class or interface declaring the underlying fieldIllegalAccessException
- if the field cannot be read
-
isBasicValueType
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
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
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
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
Get the raw type from a Type.- Parameters:
type
- the type- Returns:
- the raw type
- Throws:
IllegalArgumentException
- if passed a TypeVariable or anything other than aClass<?>
reference orParameterizedType
.
-
fieldIsSerializable
static boolean fieldIsSerializable(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
-