Package org.apache.fulcrum.json.gson
Class GSONBuilderService
- java.lang.Object
-
- org.apache.avalon.framework.logger.AbstractLogEnabled
-
- org.apache.fulcrum.json.gson.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
-
-
Field Summary
-
Fields inherited from interface org.apache.fulcrum.json.JsonService
ROLE, SERVICE_NAME
-
-
Constructor Summary
Constructors Constructor Description GSONBuilderService()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description JsonService
addAdapter(String name, Class target, Class adapter)
registering an adapter.JsonService
addAdapter(String name, Class target, Object adapter)
registering an adaptervoid
configure(org.apache.avalon.framework.configuration.Configuration conf)
<T> T
deSer(String json, Class<T> type)
Deserializing a JSON string<T> Collection<T>
deSerCollection(String json, Object collectionType, Class<T> elementType)
This is to deserialize collections.void
getJsonService()
void
initialize()
JsonService
registerTypeAdapter(Object serdeser, Type type)
String
ser(Object src)
Serializes a Java objectString
ser(Object src, Boolean refreshCache)
<T> String
ser(Object src, Class<T> type)
Serializes a Java object<T> String
ser(Object src, Class<T> type, Boolean refreshCache)
Serialize an objectString
serializeAllExceptFilter(Object src, Boolean notused, String... filterAttr)
Class Filter is derived from param src object class.<T> String
serializeAllExceptFilter(Object src, Class<T> filterClass, Boolean clearCache, String... filterAttr)
Serialize all object properties excluding provided filters attributes.<T> String
serializeAllExceptFilter(Object src, Class<T> filterClass, String... filterAttr)
Class Filter is derived from param src object class.String
serializeAllExceptFilter(Object src, String... filterAttr)
String
serializeOnlyFilter(Object src, Boolean notused, String... filterAttr)
Custom method.<T> String
serializeOnlyFilter(Object arg0, Class<T> arg1, Boolean arg2, String... arg3)
Serialize only object properties where filter attributes are provided.<T> String
serializeOnlyFilter(Object src, Class<T> filterClass, String... filterAttr)
String
serializeOnlyFilter(Object src, String... filterAttr)
Custom method without caching.void
setDateFormat(String dfStr)
Alternative method to callingregisterTypeAdapter(Object, Type)
Note: Always use either this direct format call or the other adapter register call, otherwise inconsistencies may occur!void
setDateFormat(DateFormat df)
-
-
-
Constructor Detail
-
GSONBuilderService
public GSONBuilderService()
-
-
Method Detail
-
ser
public String ser(Object src) throws Exception
Description copied from interface:JsonService
Serializes a Java object- Specified by:
ser
in interfaceJsonService
- 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 interfaceJsonService
- Type Parameters:
T
- The class type- Parameters:
src
- The Java object to be serializedtype
- 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 interfaceJsonService
- Type Parameters:
T
- class type of the object- Parameters:
json
- the JSON string to be deserializedtype
- 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 interfaceJsonService
- Type Parameters:
T
- class type of the object- Parameters:
json
- The JSON string to be deserializedcollectionType
- 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 String serializeOnlyFilter(Object src, String... filterAttr) throws Exception
Description copied from interface:JsonService
Custom method without caching. Caching is set tofalse
for this method call.- Specified by:
serializeOnlyFilter
in interfaceJsonService
- Parameters:
src
- the source objectfilterAttr
- filter attributes- Returns:
- JSON string
- Throws:
Exception
- if fails- See Also:
refreshFilter is set to true for this method call.
-
serializeOnlyFilter
public String serializeOnlyFilter(Object src, Boolean notused, String... filterAttr) throws Exception
Description copied from interface:JsonService
Custom method. Caching key is derived from param src object class.- Specified by:
serializeOnlyFilter
in interfaceJsonService
- Parameters:
src
- The Java object to serializenotused
- The Boolean value, not null. If it istrue
, cleans cache and the custom filter after serialization.refreshFilter
is set totrue
for this method call.filterAttr
- filter attributes- Returns:
- JSON string
- Throws:
Exception
- if fails- See Also:
JsonService.serializeOnlyFilter(Object, Class, Boolean, String...)
-
serializeOnlyFilter
public <T> String serializeOnlyFilter(Object src, Class<T> filterClass, String... filterAttr) throws Exception
- Specified by:
serializeOnlyFilter
in interfaceJsonService
- Type Parameters:
T
- class type of the object- Parameters:
src
- The Java object to serializefilterClass
- The filter classfilterAttr
- filter attributes- Returns:
- JSON string
- Throws:
Exception
- if fails- See Also:
Caching is set to false for this method call.
-
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 interfaceJsonService
- Type Parameters:
T
- class type of the object- Parameters:
arg0
- The Java object to serializearg1
- 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 istrue
, 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 interfaceJsonService
- Parameters:
name
- The name of the adaptertarget
- The target class for this adapteradapter
- 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 reinitializeGsonBuilder
using @linkinitialize()
, although a new Adapter with the same target overwrites the previously defined.- Specified by:
addAdapter
in interfaceJsonService
- Parameters:
name
- The name of the adaptertarget
- The target class for this adapteradapter
- 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 tofalse
for this method call.- Specified by:
serializeAllExceptFilter
in interfaceJsonService
- 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 interfaceJsonService
- 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
- Iftrue
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
-
serializeAllExceptFilter
public String serializeAllExceptFilter(Object src, String... filterAttr) throws Exception
- Specified by:
serializeAllExceptFilter
in interfaceJsonService
- 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.filterAttr
- The bean attributes which should not be serialized- Returns:
- JSON string
- Throws:
Exception
- If JSON serialization or filter registration fails- See Also:
JsonService.serializeAllExceptFilter(Object, Class, Boolean, String...)
-
serializeAllExceptFilter
public String serializeAllExceptFilter(Object src, Boolean notused, String... filterAttr) throws Exception
Description copied from interface:JsonService
Class Filter is derived from param src object class.- Specified by:
serializeAllExceptFilter
in interfaceJsonService
- 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.notused
- Iftrue
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- See Also:
JsonService.serializeAllExceptFilter(Object, Class, Boolean, String...)
-
ser
public String ser(Object src, Boolean refreshCache) throws Exception
- Specified by:
ser
in interfaceJsonService
- Parameters:
src
- the java object to be serialized, not null.refreshCache
- a boolean value, not null. Iftrue
, 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 interfaceJsonService
- Type Parameters:
T
- class type of the object- Parameters:
src
- The source objecttype
- The class type of the objectrefreshCache
- Iftrue
, try to clean cache after serialization For other attributes @seeJsonService.ser(Object, Class)
- Returns:
- the serialized class
- Throws:
Exception
- if the JSON serialization fails
-
registerTypeAdapter
public JsonService registerTypeAdapter(Object serdeser, Type type)
-
setDateFormat
public void setDateFormat(String dfStr)
Alternative method to callingregisterTypeAdapter(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
-
setDateFormat
public void setDateFormat(DateFormat df)
- Specified by:
setDateFormat
in interfaceJsonService
- Parameters:
df
- TheDateFormat
to be used by the JsonService, not null. It could be provided by component configuration too.
-
getJsonService
public void getJsonService() throws InstantiationException
- Throws:
InstantiationException
-
configure
public void configure(org.apache.avalon.framework.configuration.Configuration conf) throws org.apache.avalon.framework.configuration.ConfigurationException
- Specified by:
configure
in interfaceorg.apache.avalon.framework.configuration.Configurable
- Throws:
org.apache.avalon.framework.configuration.ConfigurationException
-
initialize
public void initialize() throws Exception
- Specified by:
initialize
in interfaceorg.apache.avalon.framework.activity.Initializable
- Throws:
Exception
-
-