Class JsonUtils
java.lang.Object
com.kingsrook.qqq.backend.core.utils.JsonUtils
Utility class for working with JSON.
See: https://www.baeldung.com/jackson-vs-gson
-
Nested Class Summary
Nested Classes -
Field Summary
FieldsModifier and TypeFieldDescription/////////////////////////////////////////////////////////////////// -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic org.json.JSONArray
returns a JSONArray with a single value if given value looks like an object otherwise returns the JSONArraystatic boolean
looksLikeArray
(String json) Check if a string looks like it could be a JSON array (e.g., starts with "[" (plus optional whitespace))static boolean
looksLikeObject
(String json) 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 QRecordstatic String
prettyPrint
(String json) Serialize any object into a "pretty" / formatted JSON String.static String
Serialize any object into a JSON String.static String
Deprecated.static org.json.JSONArray
toJSONArray
(String json) 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
toJSONObject
(String json) De-serialize a json string into a JSONObject (string must start with "{")static <T> T
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
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
toPrettyJson
(Object object) Serialize any object into a "pretty" / formatted JSON String.
-
Field Details
-
nullKeyToEmptyStringSerializer
///////////////////////////////////////////////////////////////////
-
-
Constructor Details
-
JsonUtils
public JsonUtils()
-
-
Method Details
-
toJson
-
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
-
toPrettyJson
-
prettyPrint
-
toObject
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
De-serialize a json string into a JSONObject (string must start with "{")- Throws:
org.json.JSONException
-
toJSONArray
De-serialize a json string into a JSONArray (string must start with "[")- Throws:
org.json.JSONException
-
getJSONArrayFromJSONObjectOrJSONArray
returns a JSONArray with a single value if given value looks like an object otherwise returns the JSONArray -
looksLikeObject
Check if a string looks like it could be a JSON object (e.g., starts with "{" (plus optional whitespace)) -
looksLikeArray
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
-