Interface JsonManager
- All Known Implementing Classes:
SpincastGsonManager
,SpincastJsonManager
Json
strings and objects.-
Method Summary
Modifier and TypeMethodDescriptionTries to clone an object to aJsonObject
or aJsonArray
, if the object is not of a primitive type (orBigDecimal
).Tries to clone an object to aJsonObject
or aJsonArray
, if the object is not of a primitive type (orBigDecimal
).cloneJsonArray
(JsonArray jsonArray, boolean mutable) Deep copy of theJsonArray
, so any modification to the original won't affect the clone, and vice-versa.cloneJsonObject
(JsonObject jsonObject, boolean mutable) Deep copy of theJsonObject
, so any modification to the original won't affect the clone, and vice-versa.convertToJsonDate
(Date date) Converts a Date to aJson
date format.convertToNativeType
(Object originalObject) Convert a random object to a valid native JsonObject type, if it's not already.create()
Creates an emptyJsonObject
Creates an emptyJsonArray
.createForm
(String formName) Creates an emptyForm
, which is a JsonObject + a validations container.enumsToFriendlyJsonArray
(Enum<?>[] enumValues) Convert the enums to an array ofJsonObjects
that have a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum).enumToFriendlyJsonObject
(Enum<?> enumValue) Convert the enum value to aJsonObject
that has a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum)fromClasspathFile
(String path) Creates aJsonObject
from a classpath Json file.fromCollectionToJsonArray
(Collection<?> collection) Creates aJsonArray
from a random collection.Creates aJsonObject
from a Json file.Creates aJsonObject
from the path of a file, on the file system.fromInputStream
(InputStream inputStream) Creates aJsonObject
from an inputStream.<T> T
fromInputStream
(InputStream inputStream, Class<T> clazz) Creates an instance of the specifiedT
type from aJson
inputStream.fromInputStreamArray
(InputStream inputStream) Creates aJsonArray
from an inputStream.fromInputStreamToMap
(InputStream inputStream) Creates aMap<String, Object>
from aJson
inputStream.fromListArray
(List<?> elements) Creates aJsonArray
from aList
of elements.Creates an emptyJsonObject
based on the specified Map.Creates aJsonObject
based on the specified Map.fromObject
(Object object) Creates aJsonObject
from a random object..fromString
(String jsonString) Creates aJsonObject
from aJson
String.<T> T
fromString
(String jsonString, Class<T> clazz) Creates an instance of the specifiedT
type from aJson
String.fromStringArray
(String jsonString) Creates aJsonArray
from aJson
String.fromStringToMap
(String jsonString) Creates aMap<String, Object>
from aJson
String.getElementAtJsonPath
(JsonArray array, String jsonPath) Gets an element from theJsonArray
at the specifiedJsonPath
.getElementAtJsonPath
(JsonArray array, String jsonPath, Object defaultElement) Gets an element from theJsonArray
at the specifiedJsonPath
.getElementAtJsonPath
(JsonObject obj, String jsonPath) Gets an element from theJsonObject
at the specifiedJsonPath
.getElementAtJsonPath
(JsonObject obj, String jsonPath, Object defaultElement) Gets an element from theJsonObject
at the specifiedJsonPath
.boolean
isElementExists
(JsonArray array, String jsonPath) Does the array contain an element at the specifiedJsonPath
(even ifnull
)?boolean
isElementExists
(JsonObject obj, String jsonPath) Does the object contain an element at the specifiedJsonPath
(even ifnull
)?parseDateFromJson
(String str) Converts aJson
date (ISO-8601) to a JavaUTC
date.void
putElementAtJsonPath
(JsonObjectOrArray obj, String jsonPath, Object element) Puts an element in the object at the specifiedJsonPath
.void
putElementAtJsonPath
(JsonObjectOrArray obj, String jsonPath, Object element, boolean clone) Puts a clone of the element in the object at the specifiedJsonPath
.void
removeElementAtJsonPath
(JsonArray array, String jsonPath) Removes an element at the specifiedJsonPath
from the array.void
removeElementAtJsonPath
(JsonObject obj, String jsonPath) Removes an element at the specifiedJsonPath
from the object.toJsonString
(Object obj) Gets theJson
String representation of the specified object.toJsonString
(Object obj, boolean pretty) Gets theJson
String representation of the specified object.
-
Method Details
-
create
JsonObject create()Creates an emptyJsonObject
-
createArray
JsonArray createArray()Creates an emptyJsonArray
. -
fromObject
Creates aJsonObject
from a random object.. -
fromString
Creates aJsonObject
from aJson
String.- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromMap
Creates an emptyJsonObject
based on the specified Map. An attempt will be made to create a deep copy of every elements so a modification won't affect any external references and vice-versa.The keys will be used as is, not parsed as JsonPaths.
- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromMap
Creates aJsonObject
based on the specified Map. An attempt will be made to create a deep copy of every elements so a modification won't affect any external references and vice-versa.- Parameters:
parseKeysAsJsonPaths
- iftrue
, the keys will be parsed asJsonPaths
, otherwise they will ne used as is.- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromInputStream
Creates aJsonObject
from an inputStream.- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromFile
Creates aJsonObject
from a Json file.- Returns:
- the deserialized
JsonObject
ornull
if the file isnull
or doesn't exist.
-
fromFile
Creates aJsonObject
from the path of a file, on the file system.- Returns:
- the deserialized
JsonObject
ornull
if the file isnull
or doesn't exist.
-
fromClasspathFile
Creates aJsonObject
from a classpath Json file.- Parameters:
the
- path to the classpath file. This path always starts from the root of the classpath.- Returns:
- the deserialized
JsonObject
ornull
if the file doesn't exist. - Throws:
Exception
- if the path isnull
-
fromStringToMap
Creates aMap<String, Object>
from aJson
String.- Returns:
- the
Map
version of the parameter ornull
if the parameter isnull
.
-
fromInputStreamToMap
Creates aMap<String, Object>
from aJson
inputStream.- Returns:
- the
Map
version of the parameter ornull
if the parameter isnull
.
-
fromString
Creates an instance of the specifiedT
type from aJson
String.- Returns:
- the deserialized version of the
parameter or
null
if the parameter isnull
.
-
fromInputStream
Creates an instance of the specifiedT
type from aJson
inputStream.- Returns:
- the deserialized version of the
parameter or
null
if the parameter isnull
.
-
fromCollectionToJsonArray
Creates aJsonArray
from a random collection. -
fromStringArray
Creates aJsonArray
from aJson
String.- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
fromListArray
Creates aJsonArray
from aList
of elements.- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
fromInputStreamArray
Creates aJsonArray
from an inputStream.- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
createForm
Creates an emptyForm
, which is a JsonObject + a validations container. -
toJsonString
Gets theJson
String representation of the specified object. -
toJsonString
Gets theJson
String representation of the specified object.- Parameters:
pretty
- iftrue
, the generated String will be formatted.
-
parseDateFromJson
Converts aJson
date (ISO-8601) to a JavaUTC
date. -
convertToJsonDate
Converts a Date to aJson
date format. -
convertToNativeType
Convert a random object to a valid native JsonObject type, if it's not already. -
clone
Tries to clone an object to aJsonObject
or aJsonArray
, if the object is not of a primitive type (orBigDecimal
).The cloning is made by serializing the Object to a Json string, and deserializing back. So any (de)serialization rules apply.
The non primitive object will be cloned to a
JsonArray
if it is a :- JsonArray
- Collection
- Array
-
clone
Tries to clone an object to aJsonObject
or aJsonArray
, if the object is not of a primitive type (orBigDecimal
).The cloning is made by serializing the Object to a Json string, and deserializing back. So any (de)serialization rules apply.
The non primitive object will be cloned to a
JsonArray
if it is a :- JsonArray
- Collection
- Array
- Parameters:
mutable
- iffalse
, the resulting Object and all its potential children will be immutable.
-
cloneJsonObject
Deep copy of theJsonObject
, so any modification to the original won't affect the clone, and vice-versa.Note that if the current object is immutable and the
mutable
parameter is set tofalse
, then the current object will be returned as is, since no cloning is required.- Parameters:
mutable
- iftrue
the resulting array and all its children will be mutable, otherwise they will all be immutable.
-
cloneJsonArray
Deep copy of theJsonArray
, so any modification to the original won't affect the clone, and vice-versa.Note that if the current object is immutable and the
mutable
parameter is set tofalse
, then the current array will be returned as is, since no cloning is required.- Parameters:
mutable
- iftrue
the resulting array and all its children will be mutable, otherwise they will all be immutable.
-
getElementAtJsonPath
Gets an element from theJsonObject
at the specifiedJsonPath
.- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Gets an element from theJsonObject
at the specifiedJsonPath
.- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Gets an element from theJsonArray
at the specifiedJsonPath
.- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Gets an element from theJsonArray
at the specifiedJsonPath
.- Returns:
- the element or
null
if not found.
-
putElementAtJsonPath
Puts an element in the object at the specifiedJsonPath
.All the hierarchy to the end of the
JsonPath
is created if required. -
putElementAtJsonPath
Puts a clone of the element in the object at the specifiedJsonPath
.All the hierarchy to the end of the
JsonPath
is created if required. -
removeElementAtJsonPath
Removes an element at the specifiedJsonPath
from the object. -
removeElementAtJsonPath
Removes an element at the specifiedJsonPath
from the array. -
isElementExists
Does the object contain an element at the specifiedJsonPath
(even ifnull
)? -
isElementExists
Does the array contain an element at the specifiedJsonPath
(even ifnull
)? -
enumToFriendlyJsonObject
Convert the enum value to aJsonObject
that has a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum) -
enumsToFriendlyJsonArray
Convert the enums to an array ofJsonObjects
that have a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum).
-