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 JsonServiceaddAdapter(String name, Class target, Class adapter)registering an adapter.JsonServiceaddAdapter(String name, Class target, Object adapter)registering an adaptervoidconfigure(org.apache.avalon.framework.configuration.Configuration conf)<T> TdeSer(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.voidgetJsonService()voidinitialize()JsonServiceregisterTypeAdapter(Object serdeser, Type type)Stringser(Object src)Serializes a Java objectStringser(Object src, Boolean refreshCache)<T> Stringser(Object src, Class<T> type)Serializes a Java object<T> Stringser(Object src, Class<T> type, Boolean refreshCache)Serialize an objectStringserializeAllExceptFilter(Object src, Boolean notused, String... filterAttr)Class Filter is derived from param src object class.<T> StringserializeAllExceptFilter(Object src, Class<T> filterClass, Boolean clearCache, String... filterAttr)Serialize all object properties excluding provided filters attributes.<T> StringserializeAllExceptFilter(Object src, Class<T> filterClass, String... filterAttr)Class Filter is derived from param src object class.StringserializeAllExceptFilter(Object src, String... filterAttr)StringserializeOnlyFilter(Object src, Boolean notused, String... filterAttr)Custom method.<T> StringserializeOnlyFilter(Object arg0, Class<T> arg1, Boolean arg2, String... arg3)Serialize only object properties where filter attributes are provided.<T> StringserializeOnlyFilter(Object src, Class<T> filterClass, String... filterAttr)StringserializeOnlyFilter(Object src, String... filterAttr)Custom method without caching.voidsetDateFormat(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!voidsetDateFormat(DateFormat df)
-
-
-
Constructor Detail
-
GSONBuilderService
public GSONBuilderService()
-
-
Method Detail
-
ser
public String ser(Object src) throws Exception
Description copied from interface:JsonServiceSerializes a Java object- Specified by:
serin 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:JsonServiceSerializes a Java object- Specified by:
serin 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:JsonServiceDeserializing a JSON string- Specified by:
deSerin 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:JsonServiceThis 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:
deSerCollectionin 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:JsonServiceCustom method without caching. Caching is set tofalsefor this method call.- Specified by:
serializeOnlyFilterin 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:JsonServiceCustom method. Caching key is derived from param src object class.- Specified by:
serializeOnlyFilterin interfaceJsonService- Parameters:
src- The Java object to serializenotused- The Boolean value, not null. If it istrue, cleans cache and the custom filter after serialization.refreshFilteris set totruefor 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:
serializeOnlyFilterin 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:JsonServiceSerialize only object properties where filter attributes are provided. If no filter is set, no attributes should be returned.- Specified by:
serializeOnlyFilterin 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:
addAdapterin 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 reinitializeGsonBuilderusing @linkinitialize(), although a new Adapter with the same target overwrites the previously defined.- Specified by:
addAdapterin 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:JsonServiceClass Filter is derived from param src object class.refreshFilteris set tofalsefor this method call.- Specified by:
serializeAllExceptFilterin 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:JsonServiceSerialize all object properties excluding provided filters attributes. If no filter is set, all attributes should be returned.- Specified by:
serializeAllExceptFilterin 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- Iftruecleans 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:
serializeAllExceptFilterin 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:JsonServiceClass Filter is derived from param src object class.- Specified by:
serializeAllExceptFilterin 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- Iftruecleans 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:
serin 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:JsonServiceSerialize an object- Specified by:
serin 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:
setDateFormatin interfaceJsonService- Parameters:
df- TheDateFormatto 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:
configurein interfaceorg.apache.avalon.framework.configuration.Configurable- Throws:
org.apache.avalon.framework.configuration.ConfigurationException
-
initialize
public void initialize() throws Exception
- Specified by:
initializein interfaceorg.apache.avalon.framework.activity.Initializable- Throws:
Exception
-
-