Class SpincastGsonManager
- All Implemented Interfaces:
JsonManager
-
Constructor Summary
ConstructorDescriptionSpincastGsonManager
(com.google.gson.JsonDeserializer<JsonObject> jsonObjectDeserializer, com.google.gson.JsonDeserializer<JsonArray> jsonArrayDeserializer, com.google.gson.JsonSerializer<JsonObject> jsonObjectSerializer, com.google.gson.JsonSerializer<JsonArray> jsonArraySerializer, com.google.gson.JsonSerializer<Date> dateSerializer, com.google.gson.JsonSerializer<Instant> instantSerializer, com.google.gson.JsonSerializer<BigDecimal> bigDecimalSerializer, com.google.gson.JsonSerializer<Enum<?>> enumSerializer, com.google.gson.JsonSerializer<Class<?>> classSerializer, com.google.gson.JsonSerializer<Throwable> throwableSerializer, JsonPathUtils jsonPathUtils, JsonObjectFactory jsonObjectFactory, SpincastConfig spincastConfig, SpincastUtils spincastUtils, FormFactory formFactory, com.google.inject.Provider<com.google.inject.Injector> guiceProvider) -
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.protected void
configureGsonBuilder
(com.google.gson.GsonBuilder gsonBuilder) Configure the Gson buildercom.google.gson.JsonElement
convertJsonObjectElementToGsonJsonElement
(Object originalObject) 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.protected void
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.protected com.google.gson.JsonSerializer<BigDecimal>
protected com.google.gson.JsonSerializer<Class<?>>
protected com.google.gson.JsonSerializer<Date>
getElementAtJsonPath
(JsonArray array, String jsonPath) Gets an element from theJsonArray
at the specifiedJsonPath
.getElementAtJsonPath
(JsonArray array, String jsonPath, Object defaultValue) 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 defaultValue) Gets an element from theJsonObject
at the specifiedJsonPath
.protected com.google.gson.JsonSerializer<Enum<?>>
protected FormFactory
protected String
protected String
protected String
com.google.gson.Gson
getGson()
com.google.gson.Gson
protected com.google.inject.Injector
getGuice()
protected com.google.gson.JsonSerializer<Instant>
protected com.google.gson.JsonDeserializer<JsonArray>
protected com.google.gson.JsonSerializer<JsonArray>
protected com.google.gson.JsonDeserializer<JsonObject>
protected JsonObjectFactory
protected com.google.gson.JsonSerializer<JsonObject>
protected JsonPathUtils
protected int
protected Object
getObjectFromGsonJsonElement
(com.google.gson.JsonElement jsonElement) protected SpincastConfig
protected SpincastUtils
protected com.google.gson.JsonSerializer<Throwable>
protected void
injectDependencies
(Object obj) Once the deserialization of an Object is done, we inject dependencies using Guice.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 objOrArray, String jsonPath, Object value) Puts an element in the object at the specifiedJsonPath
.void
putElementAtJsonPath
(JsonObjectOrArray objOrArray, String jsonPath, Object value, boolean clone) Puts a clone of the element in the object at the specifiedJsonPath
.protected void
registerBigDecimalSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerClassSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerCustomDeserializers
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerCustomSerializers
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerDateSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerEnumSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerInstantSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerJsonArrayDeserializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerJsonArraySerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerJsonObjectDeserializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerJsonObjectSerializer
(com.google.gson.GsonBuilder gsonBuilder) protected void
registerThrowableSerializer
(com.google.gson.GsonBuilder gsonBuilder) 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.
-
Constructor Details
-
SpincastGsonManager
@Inject public SpincastGsonManager(com.google.gson.JsonDeserializer<JsonObject> jsonObjectDeserializer, com.google.gson.JsonDeserializer<JsonArray> jsonArrayDeserializer, com.google.gson.JsonSerializer<JsonObject> jsonObjectSerializer, com.google.gson.JsonSerializer<JsonArray> jsonArraySerializer, com.google.gson.JsonSerializer<Date> dateSerializer, com.google.gson.JsonSerializer<Instant> instantSerializer, com.google.gson.JsonSerializer<BigDecimal> bigDecimalSerializer, com.google.gson.JsonSerializer<Enum<?>> enumSerializer, com.google.gson.JsonSerializer<Class<?>> classSerializer, com.google.gson.JsonSerializer<Throwable> throwableSerializer, JsonPathUtils jsonPathUtils, JsonObjectFactory jsonObjectFactory, SpincastConfig spincastConfig, SpincastUtils spincastUtils, FormFactory formFactory, com.google.inject.Provider<com.google.inject.Injector> guiceProvider)
-
-
Method Details
-
getJsonObjectDeserializer
-
getJsonArrayDeserializer
-
getJsonObjectSerializer
-
getJsonArraySerializer
-
getDateSerializer
-
getInstantSerializer
-
getBigDecimalSerializer
-
getEnumSerializer
-
getClassSerializer
-
getThrowableSerializer
-
getJsonPathUtils
-
getJsonObjectFactory
-
getSpincastConfig
-
getSpincastUtils
-
getFormFactory
-
getGuice
protected com.google.inject.Injector getGuice() -
getGson
public com.google.gson.Gson getGson() -
getGsonPrettyPrinting
public com.google.gson.Gson getGsonPrettyPrinting() -
createGsons
protected void createGsons() -
configureGsonBuilder
protected void configureGsonBuilder(com.google.gson.GsonBuilder gsonBuilder) Configure the Gson builder -
registerCustomDeserializers
protected void registerCustomDeserializers(com.google.gson.GsonBuilder gsonBuilder) -
registerJsonObjectDeserializer
protected void registerJsonObjectDeserializer(com.google.gson.GsonBuilder gsonBuilder) -
registerJsonArrayDeserializer
protected void registerJsonArrayDeserializer(com.google.gson.GsonBuilder gsonBuilder) -
registerCustomSerializers
protected void registerCustomSerializers(com.google.gson.GsonBuilder gsonBuilder) -
registerJsonObjectSerializer
protected void registerJsonObjectSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerJsonArraySerializer
protected void registerJsonArraySerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerDateSerializer
protected void registerDateSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerInstantSerializer
protected void registerInstantSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerBigDecimalSerializer
protected void registerBigDecimalSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerEnumSerializer
protected void registerEnumSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerClassSerializer
protected void registerClassSerializer(com.google.gson.GsonBuilder gsonBuilder) -
registerThrowableSerializer
protected void registerThrowableSerializer(com.google.gson.GsonBuilder gsonBuilder) -
create
Description copied from interface:JsonManager
Creates an emptyJsonObject
- Specified by:
create
in interfaceJsonManager
-
createArray
Description copied from interface:JsonManager
Creates an emptyJsonArray
.- Specified by:
createArray
in interfaceJsonManager
-
fromObject
Description copied from interface:JsonManager
Creates aJsonObject
from a random object..- Specified by:
fromObject
in interfaceJsonManager
-
fromMap
Description copied from interface:JsonManager
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.
- Specified by:
fromMap
in interfaceJsonManager
- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromMap
Description copied from interface:JsonManager
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.- Specified by:
fromMap
in interfaceJsonManager
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
Description copied from interface:JsonManager
Creates aJsonObject
from an inputStream.- Specified by:
fromInputStream
in interfaceJsonManager
- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
getFromInputStreamEncoding
-
fromFile
Description copied from interface:JsonManager
Creates aJsonObject
from a Json file.- Specified by:
fromFile
in interfaceJsonManager
- Returns:
- the deserialized
JsonObject
ornull
if the file isnull
or doesn't exist.
-
getFromFileEncoding
-
fromFile
Description copied from interface:JsonManager
Creates aJsonObject
from the path of a file, on the file system.- Specified by:
fromFile
in interfaceJsonManager
- Returns:
- the deserialized
JsonObject
ornull
if the file isnull
or doesn't exist.
-
fromClasspathFile
Description copied from interface:JsonManager
Creates aJsonObject
from a classpath Json file.- Specified by:
fromClasspathFile
in interfaceJsonManager
- Returns:
- the deserialized
JsonObject
ornull
if the file doesn't exist.
-
getFromClasspathFileEncoding
-
fromStringToMap
Description copied from interface:JsonManager
Creates aMap<String, Object>
from aJson
String.- Specified by:
fromStringToMap
in interfaceJsonManager
- Returns:
- the
Map
version of the parameter ornull
if the parameter isnull
.
-
fromInputStreamToMap
Description copied from interface:JsonManager
Creates aMap<String, Object>
from aJson
inputStream.- Specified by:
fromInputStreamToMap
in interfaceJsonManager
- Returns:
- the
Map
version of the parameter ornull
if the parameter isnull
.
-
fromString
Description copied from interface:JsonManager
Creates aJsonObject
from aJson
String.- Specified by:
fromString
in interfaceJsonManager
- Returns:
- the
JsonObject
version of the parameter ornull
if the parameter isnull
.
-
fromString
Description copied from interface:JsonManager
Creates an instance of the specifiedT
type from aJson
String.- Specified by:
fromString
in interfaceJsonManager
- Returns:
- the deserialized version of the
parameter or
null
if the parameter isnull
.
-
fromInputStream
Description copied from interface:JsonManager
Creates an instance of the specifiedT
type from aJson
inputStream.- Specified by:
fromInputStream
in interfaceJsonManager
- Returns:
- the deserialized version of the
parameter or
null
if the parameter isnull
.
-
fromCollectionToJsonArray
Description copied from interface:JsonManager
Creates aJsonArray
from a random collection.- Specified by:
fromCollectionToJsonArray
in interfaceJsonManager
-
fromStringArray
Description copied from interface:JsonManager
Creates aJsonArray
from aJson
String.- Specified by:
fromStringArray
in interfaceJsonManager
- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
fromListArray
Description copied from interface:JsonManager
Creates aJsonArray
from aList
of elements.- Specified by:
fromListArray
in interfaceJsonManager
- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
fromInputStreamArray
Description copied from interface:JsonManager
Creates aJsonArray
from an inputStream.- Specified by:
fromInputStreamArray
in interfaceJsonManager
- Returns:
- the
JsonArray
version of the parameter ornull
if the parameter isnull
.
-
createForm
Description copied from interface:JsonManager
Creates an emptyForm
, which is a JsonObject + a validations container.- Specified by:
createForm
in interfaceJsonManager
-
toJsonString
Description copied from interface:JsonManager
Gets theJson
String representation of the specified object.- Specified by:
toJsonString
in interfaceJsonManager
-
toJsonString
Description copied from interface:JsonManager
Gets theJson
String representation of the specified object.- Specified by:
toJsonString
in interfaceJsonManager
pretty
- iftrue
, the generated String will be formatted.
-
parseDateFromJson
Description copied from interface:JsonManager
Converts aJson
date (ISO-8601) to a JavaUTC
date.- Specified by:
parseDateFromJson
in interfaceJsonManager
-
convertToJsonDate
Description copied from interface:JsonManager
Converts a Date to aJson
date format.- Specified by:
convertToJsonDate
in interfaceJsonManager
-
convertToNativeType
Description copied from interface:JsonManager
Convert a random object to a valid native JsonObject type, if it's not already.- Specified by:
convertToNativeType
in interfaceJsonManager
-
convertJsonObjectElementToGsonJsonElement
-
getObjectFromGsonJsonElement
-
clone
Description copied from interface:JsonManager
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
- Specified by:
clone
in interfaceJsonManager
-
clone
Description copied from interface:JsonManager
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
- Specified by:
clone
in interfaceJsonManager
mutable
- iffalse
, the resulting Object and all its potential children will be immutable.
-
cloneJsonObject
Description copied from interface:JsonManager
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.- Specified by:
cloneJsonObject
in interfaceJsonManager
mutable
- iftrue
the resulting array and all its children will be mutable, otherwise they will all be immutable.
-
cloneJsonArray
Description copied from interface:JsonManager
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.- Specified by:
cloneJsonArray
in interfaceJsonManager
mutable
- iftrue
the resulting array and all its children will be mutable, otherwise they will all be immutable.
-
removeElementAtJsonPath
Description copied from interface:JsonManager
Removes an element at the specifiedJsonPath
from the object.- Specified by:
removeElementAtJsonPath
in interfaceJsonManager
-
removeElementAtJsonPath
Description copied from interface:JsonManager
Removes an element at the specifiedJsonPath
from the array.- Specified by:
removeElementAtJsonPath
in interfaceJsonManager
-
isElementExists
Description copied from interface:JsonManager
Does the object contain an element at the specifiedJsonPath
(even ifnull
)?- Specified by:
isElementExists
in interfaceJsonManager
-
isElementExists
Description copied from interface:JsonManager
Does the array contain an element at the specifiedJsonPath
(even ifnull
)?- Specified by:
isElementExists
in interfaceJsonManager
-
enumToFriendlyJsonObject
Description copied from interface:JsonManager
Convert the enum value to aJsonObject
that has a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum)- Specified by:
enumToFriendlyJsonObject
in interfaceJsonManager
-
enumsToFriendlyJsonArray
Description copied from interface:JsonManager
Convert the enums to an array ofJsonObjects
that have a ".name" property (thename()
of the enum) and a ".label" property (thetoString()
of the enum).- Specified by:
enumsToFriendlyJsonArray
in interfaceJsonManager
-
getElementAtJsonPath
Description copied from interface:JsonManager
Gets an element from theJsonObject
at the specifiedJsonPath
.- Specified by:
getElementAtJsonPath
in interfaceJsonManager
- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Description copied from interface:JsonManager
Gets an element from theJsonObject
at the specifiedJsonPath
.- Specified by:
getElementAtJsonPath
in interfaceJsonManager
- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Description copied from interface:JsonManager
Gets an element from theJsonArray
at the specifiedJsonPath
.- Specified by:
getElementAtJsonPath
in interfaceJsonManager
- Returns:
- the element or
null
if not found.
-
getElementAtJsonPath
Description copied from interface:JsonManager
Gets an element from theJsonArray
at the specifiedJsonPath
.- Specified by:
getElementAtJsonPath
in interfaceJsonManager
- Returns:
- the element or
null
if not found.
-
putElementAtJsonPath
Description copied from interface:JsonManager
Puts an element in the object at the specifiedJsonPath
.All the hierarchy to the end of the
JsonPath
is created if required.- Specified by:
putElementAtJsonPath
in interfaceJsonManager
-
putElementAtJsonPath
public void putElementAtJsonPath(JsonObjectOrArray objOrArray, String jsonPath, Object value, boolean clone) Description copied from interface:JsonManager
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.- Specified by:
putElementAtJsonPath
in interfaceJsonManager
-
getMaxNumberOfKeysWhenConvertingMapToJsonObject
protected int getMaxNumberOfKeysWhenConvertingMapToJsonObject() -
injectDependencies
Once the deserialization of an Object is done, we inject dependencies using Guice.
-