Package org.coliper.ibean.extension
Class GsonSerializerDeserializerForIBeans
- java.lang.Object
-
- org.coliper.ibean.extension.GsonSerializerDeserializerForIBeans
-
- All Implemented Interfaces:
JsonDeserializer<GsonSupport>,JsonSerializer<GsonSupport>
public class GsonSerializerDeserializerForIBeans extends Object implements JsonSerializer<GsonSupport>, JsonDeserializer<GsonSupport>
JSON serializer/deserializer used withGsonto convert IBeans with extension interfaceGsonSupportfrom and to JSON. Three prerequisites need to be fulfilled so that an IBean can be converted to and from JSON with Gson:- Gson version 2.4 or higher must be found on the classpath.
- IBean must implement extension interface
GsonSupport. - Gson used for convertion needs to have
GsonSerializerDeserializerForIBeansadded to its configuration.
Following code snippets show an example usage:
IBeanFactory factory = null; // Create a Gson JSON converter with IBean serializer/deserializer configured final Gson gson = new GsonBuilder().registerTypeHierarchyAdapter(GsonSupport.class, new GsonSerializerDeserializerForIBeans(factory)).create(); // BeanType must extend interface GsonSupport BeanType someBean = factory.create(BeanType.class); // Serialize and deserialize IBean String json = gson.toJson(someBean); BeanType deserializedBean = gson.fromJson(json, BeanType.class);
-
-
Constructor Summary
Constructors Constructor Description GsonSerializerDeserializerForIBeans()Creates a newGsonSerializerDeserializerForIBeanswith noIBeanFactorypreset.GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)Creates a newGsonSerializerDeserializerForIBeanswith a givenIBeanFactorypreset.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GsonSupportdeserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)JsonElementserialize(GsonSupport src, Type typeOfSrc, JsonSerializationContext context)
-
-
-
Constructor Detail
-
GsonSerializerDeserializerForIBeans
public GsonSerializerDeserializerForIBeans()
Creates a newGsonSerializerDeserializerForIBeanswith noIBeanFactorypreset. In this case default factory inIBeanwill be used for creation of new IBeans during deserializaion.
-
GsonSerializerDeserializerForIBeans
public GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)
Creates a newGsonSerializerDeserializerForIBeanswith a givenIBeanFactorypreset. In this case the provided factory will be used for creation of new IBeans during deserializaion.- Parameters:
iBeanFactory- the factory to use by the deserializer to create new IBeans. Ifnulldefault factory inIBeanwill be used for creation of new IBeans during deserializaion.
-
-
Method Detail
-
deserialize
public GsonSupport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
- Specified by:
deserializein interfaceJsonDeserializer<GsonSupport>- Throws:
JsonParseException
-
serialize
public JsonElement serialize(GsonSupport src, Type typeOfSrc, JsonSerializationContext context)
- Specified by:
serializein interfaceJsonSerializer<GsonSupport>
-
-