-
Notifications
You must be signed in to change notification settings - Fork 1
mintern/gson-interface
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
gson-interface This project allows a class to define its own Gson serialization and deserialization by implementing a simple interface. It also solves the common problem of wanting to leverage Gson's default serialization while adding a special field. In order for it to work, it must be registered with the Gson instance: Gson gson = new GsonBuilder() .registerTypeAdapterFactory(new InterfaceAdapterFactory()) .create(); Check out InterfaceExample.java for an illustrative example of usage. Any class that wishes to serialize itself must implement JsonSerialization<THIS_CLASS>, implementing the public JsonElement toJsonTree(GsonContext context) method. In the implementation of that method, a user can call context.toJsonTree(obj) and even context.thisToJsonTree(this). Note that calling context.toJsonTree(this) will result in infinite recursion; that is the point of using the context.thisToJsonTree(this) method Any class that wishes to have custom deserialization must implement JsonDeserialization<DESERIALIZER>, where DESERIALIZER implements JsonDeserializes<CLASS_TO_DESERIALIZE>. The deserializer has to have a default constructor and it has to implement the CLASS_TO_DESERIALIZE fromJson(JsonElement json, GsonContext context) method. In the implementation of that method, a user can call context.fromJsonTree(json, objClass), context.thisFromJsonTree(json), or even context.thisFromJsonTree(json, subclassOfCLASS_TO_DESERIALIZE). As with toJsonTree, calling context.fromJsonTree(json, CLASS_TO_DESERIALIZE.class) will result in infinite recursion; use context.thisFromJsonTree(json) instead. This code has been tested heavily against gson-2.1 and recently updated to incorporate changes in gson-2.2.1. This project came largely from a discussion on the google-gson issue tracker: http://code.google.com/p/google-gson/issues/detail?id=400 The example classes (Drink and MixedDrink) came from a related disussion: http://code.google.com/p/google-gson/issues/detail?id=43
About
Allows classes to define Gson de/serialization without fussing with TypeAdapters
Resources
Stars
Watchers
Forks
Releases
No releases published
Packages 0
No packages published