public class GsonSerializerDeserializerForIBeans extends Object implements JsonSerializer<GsonSupport>, JsonDeserializer<GsonSupport>
Gson to convert IBeans with
extension interface GsonSupport from and to JSON.
Three prerequisites need to be fulfilled so that an IBean can be converted to
and from JSON with Gson:
GsonSupport.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 and Description |
|---|
GsonSerializerDeserializerForIBeans()
Creates a new
GsonSerializerDeserializerForIBeans with no
IBeanFactory preset. |
GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)
Creates a new
GsonSerializerDeserializerForIBeans with a given
IBeanFactory preset. |
| Modifier and Type | Method and Description |
|---|---|
GsonSupport |
deserialize(JsonElement json,
Type typeOfT,
JsonDeserializationContext context) |
JsonElement |
serialize(GsonSupport src,
Type typeOfSrc,
JsonSerializationContext context) |
public GsonSerializerDeserializerForIBeans()
GsonSerializerDeserializerForIBeans with no
IBeanFactory preset. In this case default factory in
IBean will be used for creation of new IBeans during
deserializaion.public GsonSerializerDeserializerForIBeans(IBeanFactory iBeanFactory)
GsonSerializerDeserializerForIBeans with a given
IBeanFactory preset. In this case the provided factory will be
used for creation of new IBeans during deserializaion.iBeanFactory - the factory to use by the deserializer to create new IBeans.
If null default factory in IBean will be
used for creation of new IBeans during deserializaion.public GsonSupport deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
deserialize in interface JsonDeserializer<GsonSupport>JsonParseExceptionpublic JsonElement serialize(GsonSupport src, Type typeOfSrc, JsonSerializationContext context)
serialize in interface JsonSerializer<GsonSupport>