Class JsonUtils

java.lang.Object
com.kingsrook.qqq.backend.core.utils.JsonUtils

public class JsonUtils extends Object
Utility class for working with JSON. See: https://www.baeldung.com/jackson-vs-gson
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    ///////////////////////////////////////////////////////////////////
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static org.json.JSONArray
    returns a JSONArray with a single value if given value looks like an object otherwise returns the JSONArray
    static boolean
    Check if a string looks like it could be a JSON array (e.g., starts with "[" (plus optional whitespace))
    static boolean
    Check if a string looks like it could be a JSON object (e.g., starts with "{" (plus optional whitespace))
    static QRecord
    parseQRecord(org.json.JSONObject jsonObject, Map<String,QFieldMetaData> fields, boolean useBackendFieldNames)
    Convert a json object into a QRecord
    static String
    Serialize any object into a "pretty" / formatted JSON String.
    static String
    toJson(Object object)
    Serialize any object into a JSON String.
    static String
    toJson(Object object, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer)
    Deprecated.
    static org.json.JSONArray
    De-serialize a json string into a JSONArray (string must start with "[")
    static String
    toJsonCustomized(Object object, Consumer<com.fasterxml.jackson.databind.json.JsonMapper.Builder> jsonMapperCustomizer)
    Serialize any object into a JSON String - with customizations on the Jackson ObjectMapper.
    static org.json.JSONObject
    De-serialize a json string into a JSONObject (string must start with "{")
    static <T> T
    toObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference)
    De-serialize a json string into an object of the specified class.
    static <T> T
    toObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer)
    De-serialize a json string into an object of the specified class - with customizations on the Jackson ObjectMapper.
    static <T> T
    toObject(String json, Class<T> targetClass)
    De-serialize a json string into an object of the specified class.
    static <T> T
    toObject(String json, Class<T> targetClass, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer)
    De-serialize a json string into an object of the specified class - with customizations on the Jackson ObjectMapper.
    static String
    Serialize any object into a "pretty" / formatted JSON String.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

  • Constructor Details

    • JsonUtils

      public JsonUtils()
  • Method Details

    • toJson

      public static String toJson(Object object)
      Serialize any object into a JSON String. Internally using jackson - so jackson annotations apply!
    • toJson

      @Deprecated(since="since toJsonCustomized was added, which uses jackson's newer builder object for customization") public static String toJson(Object object, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer)
      Deprecated.
      Serialize any object into a JSON String - with customizations on the Jackson ObjectMapper. Internally using jackson - so jackson annotations apply!
    • toJsonCustomized

      public static String toJsonCustomized(Object object, Consumer<com.fasterxml.jackson.databind.json.JsonMapper.Builder> jsonMapperCustomizer)
      Serialize any object into a JSON String - with customizations on the Jackson ObjectMapper. Internally using jackson - so jackson annotations apply!
    • toPrettyJson

      public static String toPrettyJson(Object object)
      Serialize any object into a "pretty" / formatted JSON String. Internally using jackson - so jackson annotations apply!
    • prettyPrint

      public static String prettyPrint(String json)
      Serialize any object into a "pretty" / formatted JSON String.
    • toObject

      public static <T> T toObject(String json, Class<T> targetClass) throws IOException
      De-serialize a json string into an object of the specified class. Internally using jackson - so jackson annotations apply!
      Throws:
      IOException
    • toObject

      public static <T> T toObject(String json, Class<T> targetClass, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer) throws IOException
      De-serialize a json string into an object of the specified class - with customizations on the Jackson ObjectMapper. Internally using jackson - so jackson annotations apply!
      Throws:
      IOException
    • toObject

      public static <T> T toObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference) throws IOException
      De-serialize a json string into an object of the specified class. Internally using jackson - so jackson annotations apply!
      Throws:
      IOException
    • toObject

      public static <T> T toObject(String json, com.fasterxml.jackson.core.type.TypeReference<T> typeReference, Consumer<com.fasterxml.jackson.databind.ObjectMapper> objectMapperCustomizer) throws IOException
      De-serialize a json string into an object of the specified class - with customizations on the Jackson ObjectMapper. Internally using jackson - so jackson annotations apply!
      Throws:
      IOException
    • toJSONObject

      public static org.json.JSONObject toJSONObject(String json) throws org.json.JSONException
      De-serialize a json string into a JSONObject (string must start with "{")
      Throws:
      org.json.JSONException
    • toJSONArray

      public static org.json.JSONArray toJSONArray(String json) throws org.json.JSONException
      De-serialize a json string into a JSONArray (string must start with "[")
      Throws:
      org.json.JSONException
    • getJSONArrayFromJSONObjectOrJSONArray

      public static org.json.JSONArray getJSONArrayFromJSONObjectOrJSONArray(Object o)
      returns a JSONArray with a single value if given value looks like an object otherwise returns the JSONArray
    • looksLikeObject

      public static boolean looksLikeObject(String json)
      Check if a string looks like it could be a JSON object (e.g., starts with "{" (plus optional whitespace))
    • looksLikeArray

      public static boolean looksLikeArray(String json)
      Check if a string looks like it could be a JSON array (e.g., starts with "[" (plus optional whitespace))
    • parseQRecord

      public static QRecord parseQRecord(org.json.JSONObject jsonObject, Map<String,QFieldMetaData> fields, boolean useBackendFieldNames)
      Convert a json object into a QRecord