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 withGson
to convert IBeans with extension interfaceGsonSupport
from 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
GsonSerializerDeserializerForIBeans
added 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 newGsonSerializerDeserializerForIBeans
with noIBeanFactory
preset.GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)
Creates a newGsonSerializerDeserializerForIBeans
with a givenIBeanFactory
preset.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description GsonSupport
deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context)
JsonElement
serialize(GsonSupport src, Type typeOfSrc, JsonSerializationContext context)
-
-
-
Constructor Detail
-
GsonSerializerDeserializerForIBeans
public GsonSerializerDeserializerForIBeans()
Creates a newGsonSerializerDeserializerForIBeans
with noIBeanFactory
preset. In this case default factory inIBean
will be used for creation of new IBeans during deserializaion.
-
GsonSerializerDeserializerForIBeans
public GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)
Creates a newGsonSerializerDeserializerForIBeans
with a givenIBeanFactory
preset. 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. Ifnull
default factory inIBean
will 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:
deserialize
in interfaceJsonDeserializer<GsonSupport>
- Throws:
JsonParseException
-
serialize
public JsonElement serialize(GsonSupport src, Type typeOfSrc, JsonSerializationContext context)
- Specified by:
serialize
in interfaceJsonSerializer<GsonSupport>
-
-