Class JSONUtils

java.lang.Object
nonapi.io.github.classgraph.json.JSONUtils

public final class JSONUtils extends Object
Utils for Java serialization and deserialization.
  • Field Details

    • isAccessibleMethod

      private static Method isAccessibleMethod
    • setAccessibleMethod

      private static Method setAccessibleMethod
    • trySetAccessibleMethod

      private static Method trySetAccessibleMethod
    • ID_KEY

      static final 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 with Id.
      See Also:
    • ID_PREFIX

      static final String ID_PREFIX
      JSON object reference id prefix.
      See Also:
    • ID_SUFFIX

      static final String ID_SUFFIX
      JSON object reference id suffix.
      See Also:
    • JSON_CHAR_REPLACEMENTS

      private static final String[] JSON_CHAR_REPLACEMENTS
      JSON character-to-string escaping replacements -- see http://www.json.org/ under "string".
    • INDENT_LEVELS

      private static final String[] INDENT_LEVELS
      Lookup table for fast indenting.
  • Constructor Details

    • JSONUtils

      private JSONUtils()
      Constructor.
  • Method Details

    • isAccessible

      private static boolean isAccessible(AccessibleObject obj)
    • tryMakeAccessible

      private static boolean tryMakeAccessible(AccessibleObject obj)
    • makeAccessible

      public static boolean makeAccessible(AccessibleObject obj, ReflectionUtils reflectionUtils)
    • escapeJSONString

      static void escapeJSONString(String unsafeStr, StringBuilder buf)
      Escape a string to be surrounded in double quotes in JSON.
      Parameters:
      unsafeStr - the unsafe str
      buf - the buf
    • escapeJSONString

      public static String escapeJSONString(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, StringBuilder buf)
      Indent (depth * indentWidth) spaces.
      Parameters:
      depth - the depth
      indentWidth - the indent width
      buf - 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 object
      field - the field
      Returns:
      the field value
      Throws:
      IllegalArgumentException - if the specified object is not an instance of the class or interface declaring the underlying field
      IllegalAccessException - if the field cannot be read
    • isBasicValueType

      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).
      Parameters:
      cls - the class
      Returns:
      true, if the class is a basic value type
    • isBasicValueType

      static boolean isBasicValueType(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(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(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 Class<?> getRawType(Type type)
      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 a Class<?> reference or ParameterizedType.
    • 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 field
      onlySerializePublicFields - if true, only serialize public fields
      Returns:
      true if the field is serializable