Class JSONSerialiser


  • public class JSONSerialiser
    extends Object
    A JSONSerialiser 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 of JSONSerialiserModules 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.

    • 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 required
        strictJson - 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 serialised
        fieldsToExclude - 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 serialised
        prettyPrint - true if the object should be serialised with pretty printing
        fieldsToExclude - 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 file
        jsonGenerator - the JsonGenerator to use to write the object to
        prettyPrint - true if the object should be serialised with pretty printing
        fieldsToExclude - 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 deserialise
        clazz - 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 deserialise
        clazz - 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 - the InputStream containing the bytes of the object to deserialise
        clazz - 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 deserialise
        type - 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 - the InputStream containing the bytes of the object to deserialise
        type - 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 - the String 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()