public class JsonUtil
extends java.lang.Object
Developers can makes use of methods from JsonParserBuilder for
creating an instance of the utility class depending upon the necessary
customizations.
For default JSON parsing:
JsonUtil.JsonParserBuilder.defaultJsonParser() - Returns an instance of
default Json utility which ignores null values and disables HTML
escapingFor customized JSON parsing:
JsonUtil.JsonParserBuilder.custom() - Returns a builder instance for
creating customized JsonUtil objectJsonUtil.JsonParserBuilder.serializeNulls() - Enables including
null values in json representationJsonUtil.JsonParserBuilder.disableHtmlEscaping() - Disables HTML escaping
in the resulting json representationJsonUtil.JsonParserBuilder.create() - Prepares the JsonUtil instance with
the customizationsDates are rendered as strings, but they do not parse back as dates.
| Modifier and Type | Class and Description |
|---|---|
static class |
JsonUtil.JsonParser |
static class |
JsonUtil.JsonParserBuilder
Builder for creating a
JsonParser instance. |
| Constructor and Description |
|---|
JsonUtil() |
| Modifier and Type | Method and Description |
|---|---|
static boolean |
containsKey(java.lang.String source,
java.lang.String key)
Returns
boolean value if the provided key is present in
the supplied JSON source |
static java.util.Map<java.lang.String,java.lang.Object> |
get(java.lang.String source,
java.util.Collection<java.lang.String> keys)
Returns a
Map containing JSON paths indicated by keys as
the key and its value, if present, from the JSON source string. |
static java.lang.Object |
get(java.lang.String source,
java.lang.String key)
Returns the value of the JSON attribute indicated by
key from the
JSON source string. |
static java.lang.Boolean |
getAsBoolean(java.lang.String source,
java.lang.String key)
Returns
boolean value from the supplied JSON source
associated with the provided key |
static java.util.List<java.lang.Object> |
getAsList(java.lang.String source,
java.lang.String key)
Returns
List from the supplied JSON source associated
with the provided key |
static java.lang.Number |
getAsNumber(java.lang.String source,
java.lang.String key)
Returns
Number from the supplied JSON source associated
with the provided key |
static java.lang.String |
getAsString(java.lang.String source,
java.lang.String key)
Returns the value of the JSON attribute indicated by
key in the
JSON source string as String. |
static boolean |
isEmpty(java.lang.String source,
java.lang.String key)
Returns
boolean value for determining the value of the
associated object with the provided key in the supplied JSON string is
empty or not. |
static int |
length(java.lang.String source)
Returns the count of element in the supplied JSON string
|
static java.lang.Object |
parse(java.lang.String json)
Deprecated.
|
static java.lang.Object |
parseJson(java.lang.String source)
Returns Java-equivalent object for the supplied JSON
source. |
static java.lang.Object |
parseJson(java.lang.String source,
java.lang.Class<?> targetClass)
Returns object of type
targetClass for the supplied JSON
source. |
static java.lang.String |
put(java.lang.String target,
java.lang.String key,
java.lang.Object value)
Associating the specified value with the specified key in the provided
JSON
target.If the the provided target previously
contained a mapping for the key, the oldvalue is replaced. |
static java.lang.String |
remove(java.lang.String target,
java.lang.String key)
Removes the mapping for the specified
key from the provided JSON
string target, if present. |
static java.lang.String |
render(java.lang.Object o)
Deprecated.
|
static java.lang.String |
toJson(java.lang.Object source)
Converts the JSON
source to JSON string. |
static java.util.List<java.lang.Object> |
toList(java.lang.String jsonArraySource)
Creates a
List from the supplied JSON jsonArraySource. |
static java.util.Map<java.lang.String,java.lang.Object> |
toMap(java.lang.String source)
Creates a
Map from the supplied JSON source. |
static java.lang.String |
toStringOrJson(java.lang.Object object)
Parses the
object and returns a string value or an equivalent
JSON representation. |
public static java.lang.String toJson(java.lang.Object source)
source to JSON string. This method will exclude
null values, if any.source - Object to convert to JSON formsource objectpublic static java.lang.Object parseJson(java.lang.String source)
source.
Below is the JSON source to Java-equivalent mapping:
| JSON Source | Returns |
| Json Array: [...] | List<Object> |
| JSON Map: {...} | Map<String, Object> |
String, Number, Boolean, primitives |
Corresponding Java equivalent class |
source - JSON string for conversionObject post JSON conversionpublic static java.lang.Object parseJson(java.lang.String source,
java.lang.Class<?> targetClass)
targetClass for the supplied JSON
source.
If targetClass is null, JsonUtil.parseJson(String) will be the
default invocation.
source - JSON string for conversiontargetClass - name of the class to which the JSON source should be converted
totargetClass, if not null, Java
equivalent Object otherwisepublic static java.util.Map<java.lang.String,java.lang.Object> toMap(java.lang.String source)
Map from the supplied JSON source.source - JSON string for conversionMap containing JSON key-value pairspublic static java.util.List<java.lang.Object> toList(java.lang.String jsonArraySource)
List from the supplied JSON jsonArraySource.jsonArraySource - JSON string for conversionList contains the array elementspublic static java.lang.Object get(java.lang.String source,
java.lang.String key)
key from the
JSON source string.source - JSON source stringkey - key to extract the associated valueObject associated with the key in the supplied
JSON stringpublic static java.util.Map<java.lang.String,java.lang.Object> get(java.lang.String source,
java.util.Collection<java.lang.String> keys)
Map containing JSON paths indicated by keys as
the key and its value, if present, from the JSON source string.source - JSON source stringkeys - keys to extract the associated valueObject associated with the key in the supplied
JSON stringpublic static java.lang.String getAsString(java.lang.String source,
java.lang.String key)
key in the
JSON source string as String.source - JSON source stringkey - key to extract the associated value stringkeypublic static java.lang.Number getAsNumber(java.lang.String source,
java.lang.String key)
Number from the supplied JSON source associated
with the provided keysource - JSON source stringkey - key to extract the associated NumberNumber with the provided keypublic static java.util.List<java.lang.Object> getAsList(java.lang.String source,
java.lang.String key)
List from the supplied JSON source associated
with the provided keysource - JSON source stringkey - key to extract the associated valuekey as
Listpublic static java.lang.Boolean getAsBoolean(java.lang.String source,
java.lang.String key)
boolean value from the supplied JSON source
associated with the provided keysource - JSON source stringkey - key to extract the associated valueboolean value with the providedkeypublic static boolean containsKey(java.lang.String source,
java.lang.String key)
boolean value if the provided key is present in
the supplied JSON sourcesource - JSON source stringkey - key to identify the presencetrue if the key is present in the supplied JSON string ,
else falsepublic static boolean isEmpty(java.lang.String source,
java.lang.String key)
boolean value for determining the value of the
associated object with the provided key in the supplied JSON string is
empty or not.source - JSON source stringkey - key to identify associating valuetrue if the value of the associated object with
the key in the supplied JSON string is empty or not present ,
else false.public static int length(java.lang.String source)
source - JSON source stringpublic static java.lang.String remove(java.lang.String target,
java.lang.String key)
key from the provided JSON
string target, if present.target - JSON target stringkey - key to be remove from targeted JSONString of updated target JSON stringpublic static java.lang.String put(java.lang.String target,
java.lang.String key,
java.lang.Object value)
target.If the the provided target previously
contained a mapping for the key, the oldvalue is replaced.target - JSON target stringkey - key to be added in target JSON stringvalue - value to be added in targeted JSON targetString with newly added/updated key value pairspublic static java.lang.String toStringOrJson(java.lang.Object object)
throws java.lang.Exception
object and returns a string value or an equivalent
JSON representation.
NOTE: This method is primarily used by the HTTP clients for converting the request body to appropriate json format.
TODO: Move this method out of here to HttpClientWrapper
once other HTTP clients are deprecated.
object - object to convert into string or jsonString value, if object is of type string,
otherwise json representationjava.lang.Exception@Deprecated
public static java.lang.String render(java.lang.Object o)
throws java.lang.Exception
Note: Please use JsonUtil.toJson(Object)
java.lang.Exception@Deprecated
public static java.lang.Object parse(java.lang.String json)
throws java.lang.Exception
JsonUtil.parseJson(String)java.lang.Exception