public class SpincastGsonManager extends Object implements JsonManager
| Constructor and Description |
|---|
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,
JsonPathUtils jsonPathUtils,
JsonObjectFactory jsonObjectFactory,
SpincastConfig spincastConfig,
SpincastUtils spincastUtils,
FormFactory formFactory,
com.google.inject.Provider<com.google.inject.Injector> guiceProvider) |
| Modifier and Type | Method and Description |
|---|---|
Object |
clone(Object originalObject)
Tries to clone an object to a
JsonObject or
a JsonArray, if the object is not of
a primitive type (or BigDecimal). |
Object |
clone(Object originalObject,
boolean mutable)
Tries to clone an object to a
JsonObject or
a JsonArray, if the object is not of
a primitive type (or BigDecimal). |
JsonArray |
cloneJsonArray(JsonArray jsonArray,
boolean mutable)
Deep copy of the
JsonArray, so any
modification to the original won't affect the
clone, and vice-versa. |
JsonObject |
cloneJsonObject(JsonObject jsonObject,
boolean mutable)
Deep copy of the
JsonObject, 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 builder
|
com.google.gson.JsonElement |
convertJsonObjectElementToGsonJsonElement(Object originalObject) |
String |
convertToJsonDate(Date date)
Converts a Date to a
Json date format. |
Object |
convertToNativeType(Object originalObject)
Convert a random object to a valid native JsonObject type, if
it's not already.
|
JsonObject |
create()
Creates an empty
JsonObject |
JsonArray |
createArray()
Creates an empty
JsonArray. |
Form |
createForm(String formName)
Creates an empty
Form, which is a JsonObject +
a validations container. |
protected void |
createGsons() |
JsonArray |
enumsToFriendlyJsonArray(Enum<?>[] enumValues)
Convert the enums to an array of
JsonObjects that
have a ".name" property (the name() of the enum)
and a ".label" property (the toString() of the
enum). |
JsonObject |
enumToFriendlyJsonObject(Enum<?> enumValue)
Convert the enum value to a
JsonObject that
has a ".name" property (the name() of the enum)
and a ".label" property (the toString() of the
enum) |
JsonObject |
fromClasspathFile(String path)
Creates a
JsonObject from a classpath
Json file. |
JsonArray |
fromCollectionToJsonArray(Collection<?> collection)
Creates a
JsonArray from a random collection. |
JsonObject |
fromFile(File jsonFile)
Creates a
JsonObject from a Json file. |
JsonObject |
fromFile(String jsonFilePath)
Creates a
JsonObject from the path of
a file, on the file system. |
JsonObject |
fromInputStream(InputStream inputStream)
Creates a
JsonObject from an inputStream. |
<T> T |
fromInputStream(InputStream inputStream,
Class<T> clazz)
Creates an instance of the specified
T type
from a Json inputStream. |
JsonArray |
fromInputStreamArray(InputStream inputStream)
Creates a
JsonArray from an inputStream. |
Map<String,Object> |
fromInputStreamToMap(InputStream inputStream)
Creates a
Map<String, Object> from a Json inputStream. |
JsonArray |
fromListArray(List<?> elements)
Creates a
JsonArray from a List
of elements. |
JsonObject |
fromMap(Map<String,?> params)
Creates an empty
JsonObject based on the specified Map. |
JsonObject |
fromMap(Map<String,?> params,
boolean parseKeysAsJsonPaths)
Creates a
JsonObject based on the specified Map. |
JsonObject |
fromObject(Object object)
Creates a
JsonObject from a random object.. |
JsonObject |
fromString(String jsonString)
Creates a
JsonObject from a Json
String. |
<T> T |
fromString(String jsonString,
Class<T> clazz)
Creates an instance of the specified
T type
from a Json String. |
JsonArray |
fromStringArray(String jsonString)
Creates a
JsonArray from a Json
String. |
Map<String,Object> |
fromStringToMap(String jsonString)
Creates a
Map<String, Object> from a Json
String. |
protected com.google.gson.JsonSerializer<BigDecimal> |
getBigDecimalSerializer() |
protected com.google.gson.JsonSerializer<Class<?>> |
getClassSerializer() |
protected com.google.gson.JsonSerializer<Date> |
getDateSerializer() |
Object |
getElementAtJsonPath(JsonArray array,
String jsonPath)
Gets an element from the
JsonArray at the
specified JsonPath. |
Object |
getElementAtJsonPath(JsonArray array,
String jsonPath,
Object defaultValue)
Gets an element from the
JsonArray at the
specified JsonPath. |
Object |
getElementAtJsonPath(JsonObject obj,
String jsonPath)
Gets an element from the
JsonObject
at the specified JsonPath. |
Object |
getElementAtJsonPath(JsonObject obj,
String jsonPath,
Object defaultValue)
Gets an element from the
JsonObject
at the specified JsonPath. |
protected com.google.gson.JsonSerializer<Enum<?>> |
getEnumSerializer() |
protected FormFactory |
getFormFactory() |
protected String |
getFromClasspathFileEncoding() |
protected String |
getFromFileEncoding() |
protected String |
getFromInputStreamEncoding() |
com.google.gson.Gson |
getGson() |
com.google.gson.Gson |
getGsonPrettyPrinting() |
protected com.google.inject.Injector |
getGuice() |
protected com.google.gson.JsonSerializer<Instant> |
getInstantSerializer() |
protected com.google.gson.JsonDeserializer<JsonArray> |
getJsonArrayDeserializer() |
protected com.google.gson.JsonSerializer<JsonArray> |
getJsonArraySerializer() |
protected com.google.gson.JsonDeserializer<JsonObject> |
getJsonObjectDeserializer() |
protected JsonObjectFactory |
getJsonObjectFactory() |
protected com.google.gson.JsonSerializer<JsonObject> |
getJsonObjectSerializer() |
protected JsonPathUtils |
getJsonPathUtils() |
protected int |
getMaxNumberOfKeysWhenConvertingMapToJsonObject() |
protected Object |
getObjectFromGsonJsonElement(com.google.gson.JsonElement jsonElement) |
protected SpincastConfig |
getSpincastConfig() |
protected SpincastUtils |
getSpincastUtils() |
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 specified
JsonPath (even if
null)? |
boolean |
isElementExists(JsonObject obj,
String jsonPath)
Does the object contain an element at
the specified
JsonPath (even if
null)? |
Date |
parseDateFromJson(String str)
Converts a
Json date (ISO-8601) to a
Java UTC date. |
void |
putElementAtJsonPath(JsonObjectOrArray objOrArray,
String jsonPath,
Object value)
Puts an element in the object at the specified
JsonPath. |
void |
putElementAtJsonPath(JsonObjectOrArray objOrArray,
String jsonPath,
Object value,
boolean clone)
Puts a clone of the element in the object at the
specified
JsonPath. |
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) |
void |
removeElementAtJsonPath(JsonArray array,
String jsonPath)
Removes an element at the specified
JsonPath from the
array. |
void |
removeElementAtJsonPath(JsonObject obj,
String jsonPath)
Removes an element at the specified
JsonPath from the
object. |
String |
toJsonString(Object obj)
Gets the
Json String representation of
the specified object. |
String |
toJsonString(Object obj,
boolean pretty)
Gets the
Json String representation of the
specified object. |
@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, JsonPathUtils jsonPathUtils, JsonObjectFactory jsonObjectFactory, SpincastConfig spincastConfig, SpincastUtils spincastUtils, FormFactory formFactory, com.google.inject.Provider<com.google.inject.Injector> guiceProvider)
protected com.google.gson.JsonDeserializer<JsonObject> getJsonObjectDeserializer()
protected com.google.gson.JsonDeserializer<JsonArray> getJsonArrayDeserializer()
protected com.google.gson.JsonSerializer<JsonObject> getJsonObjectSerializer()
protected com.google.gson.JsonSerializer<JsonArray> getJsonArraySerializer()
protected com.google.gson.JsonSerializer<Date> getDateSerializer()
protected com.google.gson.JsonSerializer<Instant> getInstantSerializer()
protected com.google.gson.JsonSerializer<BigDecimal> getBigDecimalSerializer()
protected com.google.gson.JsonSerializer<Enum<?>> getEnumSerializer()
protected com.google.gson.JsonSerializer<Class<?>> getClassSerializer()
protected JsonPathUtils getJsonPathUtils()
protected JsonObjectFactory getJsonObjectFactory()
protected SpincastConfig getSpincastConfig()
protected SpincastUtils getSpincastUtils()
protected FormFactory getFormFactory()
protected com.google.inject.Injector getGuice()
public com.google.gson.Gson getGson()
public com.google.gson.Gson getGsonPrettyPrinting()
protected void createGsons()
protected void configureGsonBuilder(com.google.gson.GsonBuilder gsonBuilder)
protected void registerCustomDeserializers(com.google.gson.GsonBuilder gsonBuilder)
protected void registerJsonObjectDeserializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerJsonArrayDeserializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerCustomSerializers(com.google.gson.GsonBuilder gsonBuilder)
protected void registerJsonObjectSerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerJsonArraySerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerDateSerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerInstantSerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerBigDecimalSerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerEnumSerializer(com.google.gson.GsonBuilder gsonBuilder)
protected void registerClassSerializer(com.google.gson.GsonBuilder gsonBuilder)
public JsonObject create()
JsonManagerJsonObjectcreate in interface JsonManagerpublic JsonArray createArray()
JsonManagerJsonArray.createArray in interface JsonManagerpublic JsonObject fromObject(Object object)
JsonManagerJsonObject from a random object..fromObject in interface JsonManagerpublic JsonObject fromMap(Map<String,?> params)
JsonManagerJsonObject 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.
fromMap in interface JsonManagerJsonObject version of the
parameter or null if the parameter is
null.public JsonObject fromMap(Map<String,?> params, boolean parseKeysAsJsonPaths)
JsonManagerJsonObject 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.fromMap in interface JsonManagerparseKeysAsJsonPaths - if true, the keys will
be parsed as JsonPaths, otherwise they will ne used
as is.JsonObject version of the
parameter or null if the parameter is
null.public JsonObject fromInputStream(InputStream inputStream)
JsonManagerJsonObject from an inputStream.fromInputStream in interface JsonManagerJsonObject version of the
parameter or null if the parameter is
null.protected String getFromInputStreamEncoding()
public JsonObject fromFile(File jsonFile)
JsonManagerJsonObject from a Json file.fromFile in interface JsonManagerJsonObject
or null if the file is
null or doesn't exist.protected String getFromFileEncoding()
public JsonObject fromFile(String jsonFilePath)
JsonManagerJsonObject from the path of
a file, on the file system.fromFile in interface JsonManagerJsonObject
or null if the file is
null or doesn't exist.public JsonObject fromClasspathFile(String path)
JsonManagerJsonObject from a classpath
Json file.fromClasspathFile in interface JsonManagerJsonObject
or null if the file doesn't exist.protected String getFromClasspathFileEncoding()
public Map<String,Object> fromStringToMap(String jsonString)
JsonManagerMap<String, Object> from a Json
String.fromStringToMap in interface JsonManagerMap version of the
parameter or null if the parameter is
null.public Map<String,Object> fromInputStreamToMap(InputStream inputStream)
JsonManagerMap<String, Object> from a Json inputStream.fromInputStreamToMap in interface JsonManagerMap version of the
parameter or null if the parameter is
null.public JsonObject fromString(String jsonString)
JsonManagerJsonObject from a Json
String.fromString in interface JsonManagerJsonObject version of the
parameter or null if the parameter is
null.public <T> T fromString(String jsonString, Class<T> clazz)
JsonManagerT type
from a Json String.fromString in interface JsonManagernull if the parameter is
null.public <T> T fromInputStream(InputStream inputStream, Class<T> clazz)
JsonManagerT type
from a Json inputStream.fromInputStream in interface JsonManagernull if the parameter is
null.public JsonArray fromCollectionToJsonArray(Collection<?> collection)
JsonManagerJsonArray from a random collection.fromCollectionToJsonArray in interface JsonManagerpublic JsonArray fromStringArray(String jsonString)
JsonManagerJsonArray from a Json
String.fromStringArray in interface JsonManagerJsonArray version of the
parameter or null if the parameter
is null.public JsonArray fromListArray(List<?> elements)
JsonManagerJsonArray from a List
of elements.fromListArray in interface JsonManagerJsonArray version of the
parameter or null if the parameter
is null.public JsonArray fromInputStreamArray(InputStream inputStream)
JsonManagerJsonArray from an inputStream.fromInputStreamArray in interface JsonManagerJsonArray version of the
parameter or null if the parameter
is null.public Form createForm(String formName)
JsonManagerForm, which is a JsonObject +
a validations container.createForm in interface JsonManagerpublic String toJsonString(Object obj)
JsonManagerJson String representation of
the specified object.toJsonString in interface JsonManagerpublic String toJsonString(Object obj, boolean pretty)
JsonManagerJson String representation of the
specified object.toJsonString in interface JsonManagerpretty - if true, the generated
String will be formatted.public Date parseDateFromJson(String str)
JsonManagerJson date (ISO-8601) to a
Java UTC date.parseDateFromJson in interface JsonManagerpublic String convertToJsonDate(Date date)
JsonManagerJson date format.convertToJsonDate in interface JsonManagerpublic Object convertToNativeType(Object originalObject)
JsonManagerconvertToNativeType in interface JsonManagerpublic com.google.gson.JsonElement convertJsonObjectElementToGsonJsonElement(Object originalObject)
protected Object getObjectFromGsonJsonElement(com.google.gson.JsonElement jsonElement)
public Object clone(Object originalObject)
JsonManagerJsonObject or
a JsonArray, if the object is not of
a primitive type (or BigDecimal).
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 :
clone in interface JsonManagerpublic Object clone(Object originalObject, boolean mutable)
JsonManagerJsonObject or
a JsonArray, if the object is not of
a primitive type (or BigDecimal).
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 :
clone in interface JsonManagermutable - if false, the resulting
Object and all its potential children
will be immutable.public JsonObject cloneJsonObject(JsonObject jsonObject, boolean mutable)
JsonManagerJsonObject, 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 to false,
then the current object will be returned as is, since no cloning
is required.
cloneJsonObject in interface JsonManagermutable - if true the resulting
array and all its children will be mutable, otherwise
they will all be immutable.public JsonArray cloneJsonArray(JsonArray jsonArray, boolean mutable)
JsonManagerJsonArray, 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 to false,
then the current array will be returned as is, since no cloning
is required.
cloneJsonArray in interface JsonManagermutable - if true the resulting
array and all its children will be mutable, otherwise
they will all be immutable.public void removeElementAtJsonPath(JsonObject obj, String jsonPath)
JsonManagerJsonPath from the
object.removeElementAtJsonPath in interface JsonManagerpublic void removeElementAtJsonPath(JsonArray array, String jsonPath)
JsonManagerJsonPath from the
array.removeElementAtJsonPath in interface JsonManagerpublic boolean isElementExists(JsonObject obj, String jsonPath)
JsonManagerJsonPath (even if
null)?isElementExists in interface JsonManagerpublic boolean isElementExists(JsonArray array, String jsonPath)
JsonManagerJsonPath (even if
null)?isElementExists in interface JsonManagerpublic JsonObject enumToFriendlyJsonObject(Enum<?> enumValue)
JsonManagerJsonObject that
has a ".name" property (the name() of the enum)
and a ".label" property (the toString() of the
enum)enumToFriendlyJsonObject in interface JsonManagerpublic JsonArray enumsToFriendlyJsonArray(Enum<?>[] enumValues)
JsonManagerJsonObjects that
have a ".name" property (the name() of the enum)
and a ".label" property (the toString() of the
enum).enumsToFriendlyJsonArray in interface JsonManagerpublic Object getElementAtJsonPath(JsonObject obj, String jsonPath)
JsonManagerJsonObject
at the specified JsonPath.getElementAtJsonPath in interface JsonManagernull if not found.public Object getElementAtJsonPath(JsonObject obj, String jsonPath, Object defaultValue)
JsonManagerJsonObject
at the specified JsonPath.getElementAtJsonPath in interface JsonManagernull if not found.public Object getElementAtJsonPath(JsonArray array, String jsonPath)
JsonManagerJsonArray at the
specified JsonPath.getElementAtJsonPath in interface JsonManagernull if not found.public Object getElementAtJsonPath(JsonArray array, String jsonPath, Object defaultValue)
JsonManagerJsonArray at the
specified JsonPath.getElementAtJsonPath in interface JsonManagernull if not found.public void putElementAtJsonPath(JsonObjectOrArray objOrArray, String jsonPath, Object value)
JsonManagerJsonPath.
All the hierarchy to the end of the JsonPath
is created if required.
putElementAtJsonPath in interface JsonManagerpublic void putElementAtJsonPath(JsonObjectOrArray objOrArray, String jsonPath, Object value, boolean clone)
JsonManagerJsonPath.
All the hierarchy to the end of the JsonPath
is created if required.
putElementAtJsonPath in interface JsonManagerprotected int getMaxNumberOfKeysWhenConvertingMapToJsonObject()
protected void injectDependencies(Object obj)
Copyright © 2019. All rights reserved.