Class GSONBuilderService

  • All Implemented Interfaces:
    org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable, org.apache.avalon.framework.logger.LogEnabled, JsonService

    public class GSONBuilderService
    extends org.apache.avalon.framework.logger.AbstractLogEnabled
    implements JsonService, org.apache.avalon.framework.activity.Initializable, org.apache.avalon.framework.configuration.Configurable
    By default multiple serialization of the same object in a single thread is not support (e.g adapter + default for the same bean / object).
    Version:
    $Id$
    Author:
    gk
    • Method Detail

      • ser

        public String ser​(Object src)
                   throws Exception
        Description copied from interface: JsonService
        Serializes a Java object
        Specified by:
        ser in interface JsonService
        Parameters:
        src - the java object to be serialized, not null.
        Returns:
        JSON string
        Throws:
        Exception - if JSON serialization fails
      • ser

        public <T> String ser​(Object src,
                              Class<T> type)
                       throws Exception
        Description copied from interface: JsonService
        Serializes a Java object
        Specified by:
        ser in interface JsonService
        Type Parameters:
        T - The class type
        Parameters:
        src - The Java object to be serialized
        type - the Java Type, which should be used for the provided object
        Returns:
        JSON string
        Throws:
        Exception - If JSON serialization fails
      • deSer

        public <T> T deSer​(String json,
                           Class<T> type)
                    throws Exception
        Description copied from interface: JsonService
        Deserializing a JSON string
        Specified by:
        deSer in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        json - the JSON string to be deserialized
        type - the Java Type to be used as a class
        Returns:
        the Java Object
        Throws:
        Exception - if JSON deserialization fails
      • deSerCollection

        public <T> Collection<T> deSerCollection​(String json,
                                                 Object collectionType,
                                                 Class<T> elementType)
                                          throws Exception
        Description copied from interface: JsonService
        This is to deserialize collections. Depending on the implementation either both collectiontype and elementType is needed or the elementType will be derived from the typed collectiontype.
        Specified by:
        deSerCollection in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        json - The JSON string to be deserialized
        collectionType - It could be just the collection or the typed collection. It may then be used to get the type for element type too. Cft. implementation tests for more details (GSON).
        elementType - The element type. This is need in any case to assure the generic checking.
        Returns:
        the generated Java Collection.
        Throws:
        Exception - if serialize collection fails
      • serializeOnlyFilter

        public <T> String serializeOnlyFilter​(Object arg0,
                                              Class<T> arg1,
                                              Boolean arg2,
                                              String... arg3)
                                       throws Exception
        Description copied from interface: JsonService
        Serialize only object properties where filter attributes are provided. If no filter is set, no attributes should be returned.
        Specified by:
        serializeOnlyFilter in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        arg0 - The Java object to serialize
        arg1 - By default filterClass is a) the class to be filtered (required for filtering list elements) b) the key in the filter object cached.
        arg2 - The Boolean value, not null. If it is true, cleans cache and the custom filter after serialization.
        arg3 - The class bean attributes which should be serialized
        Returns:
        JSON string
        Throws:
        Exception - If JSON serialization or filter registration fails
      • addAdapter

        public JsonService addAdapter​(String name,
                                      Class target,
                                      Object adapter)
                               throws Exception
        registering an adapter
        Specified by:
        addAdapter in interface JsonService
        Parameters:
        name - The name of the adapter
        target - The target class for this adapter
        adapter - The adapter/mixin for the target object (module/serializer/deserializer)
        Returns:
        A JsonService instance
        Throws:
        Exception - if adapter registration fails
        See Also:
        GsonBuilder.registerTypeAdapter(Type, Object)
      • addAdapter

        public JsonService addAdapter​(String name,
                                      Class target,
                                      Class adapter)
                               throws Exception
        registering an adapter. Unregistering could be only done by reinitialize GsonBuilder using @link initialize(), although a new Adapter with the same target overwrites the previously defined.
        Specified by:
        addAdapter in interface JsonService
        Parameters:
        name - The name of the adapter
        target - The target class for this adapter
        adapter - The adapter/mixin for the target class
        Returns:
        the JsonService instance
        Throws:
        Exception - If adapter registration fails
        See Also:
        GsonBuilder.registerTypeAdapter(Type, Object)
      • serializeAllExceptFilter

        public <T> String serializeAllExceptFilter​(Object src,
                                                   Class<T> filterClass,
                                                   String... filterAttr)
                                            throws Exception
        Description copied from interface: JsonService
        Class Filter is derived from param src object class. refreshFilter is set to false for this method call.
        Specified by:
        serializeAllExceptFilter in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        src - The Java object to serialize. By default the filtering is applied for this class. By default the class of the src object is the key for the filter object cached.
        filterClass - The class, which should be filtered out, if found as a property type.
        filterAttr - The bean attributes which should not be serialized
        Returns:
        JSON string
        Throws:
        Exception - If JSON serialization or filter registration fails
        See Also:
        refreshFilter is false.
      • serializeAllExceptFilter

        public <T> String serializeAllExceptFilter​(Object src,
                                                   Class<T> filterClass,
                                                   Boolean clearCache,
                                                   String... filterAttr)
                                            throws Exception
        Description copied from interface: JsonService
        Serialize all object properties excluding provided filters attributes. If no filter is set, all attributes should be returned.
        Specified by:
        serializeAllExceptFilter in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        src - The Java object to serialize. By default the filtering is applied for this class. By default the class of the src object is the key for the filter object cached.
        filterClass - The class, which should be filtered out, if found as a property type.
        clearCache - If true cleans filter (clean cache and custom filter for this filterClass) after serialization.
        filterAttr - The bean attributes which should not be serialized
        Returns:
        JSON string
        Throws:
        Exception - If JSON serialization or filter registration fails
      • ser

        public String ser​(Object src,
                          Boolean refreshCache)
                   throws Exception
        Specified by:
        ser in interface JsonService
        Parameters:
        src - the java object to be serialized, not null.
        refreshCache - a boolean value, not null. If true, try to refresh cache after serialization
        Returns:
        serialized object
        Throws:
        Exception - generic exception
      • ser

        public <T> String ser​(Object src,
                              Class<T> type,
                              Boolean refreshCache)
                       throws Exception
        Description copied from interface: JsonService
        Serialize an object
        Specified by:
        ser in interface JsonService
        Type Parameters:
        T - class type of the object
        Parameters:
        src - The source object
        type - The class type of the object
        refreshCache - If true, try to clean cache after serialization For other attributes @see JsonService.ser(Object, Class)
        Returns:
        the serialized class
        Throws:
        Exception - if the JSON serialization fails
      • setDateFormat

        public void setDateFormat​(String dfStr)
        Alternative method to calling registerTypeAdapter(Object, Type) Note: Always use either this direct format call or the other adapter register call, otherwise inconsistencies may occur!
        Parameters:
        dfStr - date format string
      • configure

        public void configure​(org.apache.avalon.framework.configuration.Configuration conf)
                       throws org.apache.avalon.framework.configuration.ConfigurationException
        Specified by:
        configure in interface org.apache.avalon.framework.configuration.Configurable
        Throws:
        org.apache.avalon.framework.configuration.ConfigurationException
      • initialize

        public void initialize()
                        throws Exception
        Specified by:
        initialize in interface org.apache.avalon.framework.activity.Initializable
        Throws:
        Exception