Class JSONSerialiser
- java.lang.Object
-
- uk.gov.gchq.gaffer.jsonserialisation.JSONSerialiser
-
public class JSONSerialiser extends Object
AJSONSerialiser
provides the ability to serialise and deserialise to/from JSON. The serialisation is set to not include nulls or default values.JSONSerialiser is a singleton. The behaviour of the
ObjectMapper
can be configured by extending this class and configuring the ObjectMapper. Child classes must has a default no argument constructor. You will then need to set the gaffer.serialiser.json.class property in your StoreProperties or as a System Property. You can also add modules to the ObjectMapper by writing an implementation ofJSONSerialiserModules
and registering it using the gaffer.serialiser.json.modules property in your StoreProperties or as a System Property.Once the singleton instance has been instantiated it will not be updated, unless update() or update(jsonSerialiserClass, jsonSerialiserModules) is called. An update will be done automatically in the REST API when it is first initialised and also when a Store is initialised.
-
-
Field Summary
Fields Modifier and Type Field Description static String
DEFAULT_SERIALISER_CLASS_NAME
static String
FILTER_FIELDS_BY_NAME
static String
JSON_SERIALISER_CLASS_KEY
static String
JSON_SERIALISER_MODULES
CSV ofJSONSerialiserModules
class names.static String
STRICT_JSON
static boolean
STRICT_JSON_DEFAULT
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
addSimpleClassNames(boolean includeSubtypes, Class<?>... classes)
static boolean
canHandle(Class<?> clazz)
static com.fasterxml.jackson.databind.ObjectMapper
createDefaultMapper()
static <T> T
deserialise(byte[] bytes, com.fasterxml.jackson.core.type.TypeReference<T> type)
static <T> T
deserialise(byte[] bytes, Class<T> clazz)
static <T> T
deserialise(InputStream stream, com.fasterxml.jackson.core.type.TypeReference<T> type)
static <T> T
deserialise(InputStream stream, Class<T> clazz)
static <T> T
deserialise(String json, Class<T> clazz)
static com.fasterxml.jackson.databind.ser.FilterProvider
getFilterProvider(String... fieldsToExclude)
static JSONSerialiser
getInstance()
static com.fasterxml.jackson.databind.JsonNode
getJsonNodeFromString(String content)
static com.fasterxml.jackson.databind.ObjectMapper
getMapper()
static byte[]
serialise(Object object, boolean prettyPrint, String... fieldsToExclude)
Serialises an object.static void
serialise(Object object, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean prettyPrint, String... fieldsToExclude)
Serialises an object using the provided json generator.static byte[]
serialise(Object object, String... fieldsToExclude)
Serialises an object.static void
update()
static void
update(String jsonSerialiserClass, String jsonSerialiserModules, Boolean strictJson)
Update the json serialiser with the provided custom properties.
-
-
-
Field Detail
-
JSON_SERIALISER_CLASS_KEY
public static final String JSON_SERIALISER_CLASS_KEY
- See Also:
- Constant Field Values
-
JSON_SERIALISER_MODULES
public static final String JSON_SERIALISER_MODULES
CSV ofJSONSerialiserModules
class names. These modules will be added to theObjectMapper
.- See Also:
- Constant Field Values
-
DEFAULT_SERIALISER_CLASS_NAME
public static final String DEFAULT_SERIALISER_CLASS_NAME
-
STRICT_JSON
public static final String STRICT_JSON
- See Also:
- Constant Field Values
-
STRICT_JSON_DEFAULT
public static final boolean STRICT_JSON_DEFAULT
- See Also:
- Constant Field Values
-
FILTER_FIELDS_BY_NAME
public static final String FILTER_FIELDS_BY_NAME
- See Also:
- Constant Field Values
-
-
Method Detail
-
addSimpleClassNames
public static void addSimpleClassNames(boolean includeSubtypes, Class<?>... classes)
-
update
public static void update(String jsonSerialiserClass, String jsonSerialiserModules, Boolean strictJson)
Update the json serialiser with the provided custom properties.- Parameters:
jsonSerialiserClass
- the json serialiser class to use (or null to use the default)jsonSerialiserModules
- any extra json serialiser modules requiredstrictJson
- true if strict json conversion should be used
-
update
public static void update()
-
createDefaultMapper
public static com.fasterxml.jackson.databind.ObjectMapper createDefaultMapper()
-
getFilterProvider
public static com.fasterxml.jackson.databind.ser.FilterProvider getFilterProvider(String... fieldsToExclude)
-
canHandle
public static boolean canHandle(Class<?> clazz)
- Parameters:
clazz
- the clazz of the object to be serialised/deserialised- Returns:
- true if the clazz can be serialised/deserialised
-
serialise
public static byte[] serialise(Object object, String... fieldsToExclude) throws SerialisationException
Serialises an object.- Parameters:
object
- the object to be serialisedfieldsToExclude
- optional property names to exclude from the json- Returns:
- the provided object serialised into bytes
- Throws:
SerialisationException
- if the object fails to be serialised
-
serialise
public static byte[] serialise(Object object, boolean prettyPrint, String... fieldsToExclude) throws SerialisationException
Serialises an object.- Parameters:
object
- the object to be serialisedprettyPrint
- true if the object should be serialised with pretty printingfieldsToExclude
- optional property names to exclude from the json- Returns:
- the provided object serialised (with pretty printing) into bytes
- Throws:
SerialisationException
- if the object fails to serialise
-
serialise
public static void serialise(Object object, com.fasterxml.jackson.core.JsonGenerator jsonGenerator, boolean prettyPrint, String... fieldsToExclude) throws SerialisationException
Serialises an object using the provided json generator.- Parameters:
object
- the object to be serialised and written to filejsonGenerator
- theJsonGenerator
to use to write the object toprettyPrint
- true if the object should be serialised with pretty printingfieldsToExclude
- optional property names to exclude from the json- Throws:
SerialisationException
- if the object fails to serialise
-
deserialise
public static <T> T deserialise(String json, Class<T> clazz) throws SerialisationException
- Type Parameters:
T
- the type of the object- Parameters:
json
- the json of the object to deserialiseclazz
- the class of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the json fails to deserialise
-
deserialise
public static <T> T deserialise(byte[] bytes, Class<T> clazz) throws SerialisationException
- Type Parameters:
T
- the type of the object- Parameters:
bytes
- the bytes of the object to deserialiseclazz
- the class of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the bytes fail to deserialise
-
deserialise
public static <T> T deserialise(InputStream stream, Class<T> clazz) throws SerialisationException
- Type Parameters:
T
- the type of the object- Parameters:
stream
- theInputStream
containing the bytes of the object to deserialiseclazz
- the class of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the bytes fail to deserialise
-
deserialise
public static <T> T deserialise(byte[] bytes, com.fasterxml.jackson.core.type.TypeReference<T> type) throws SerialisationException
- Type Parameters:
T
- the type of the object- Parameters:
bytes
- the bytes of the object to deserialisetype
- the type reference of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the bytes fail to deserialise
-
deserialise
public static <T> T deserialise(InputStream stream, com.fasterxml.jackson.core.type.TypeReference<T> type) throws SerialisationException
- Type Parameters:
T
- the type of the object- Parameters:
stream
- theInputStream
containing the bytes of the object to deserialisetype
- the type reference of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the bytes fail to deserialise
-
getJsonNodeFromString
public static com.fasterxml.jackson.databind.JsonNode getJsonNodeFromString(String content) throws SerialisationException
- Parameters:
content
- theString
containing the bytes of the object to deserialise- Returns:
- the deserialised object
- Throws:
SerialisationException
- if the bytes fail to deserialise
-
getMapper
public static com.fasterxml.jackson.databind.ObjectMapper getMapper()
-
getInstance
public static JSONSerialiser getInstance()
-
-