FranzGames / google-gson

Automatically exported from code.google.com/p/google-gson
0 stars 0 forks source link

Add support for proxy type adapters #334

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Sometimes it's useful for the logical serialization of a type to be quite 
different from its physical structure.  I've come up with a simple mechanism 
for handling this situation: use of a "proxy class" for serialization and 
deserialization.

The way this works is that the programmer provides a class that will 
participate in serialization in place of the original type.  This "proxy class" 
has a constructor that can be called with an instance of the original type; 
then the resulting proxy instance is serialized.  On deserialization, the proxy 
class provides a no-argument constructor; the type adapter deserializes into a 
proxy instance, then converts it to a type instance using the required #build() 
method.

Attached are three files: (1) a definition of a type proxy; (2) a type adapter 
that implements this proxy behavior; and (3) a usage example.

Original issue reported on code.google.com by c...@google.com on 17 Jun 2011 at 10:24

Attachments:

GoogleCodeExporter commented 9 years ago
Neat. This reminds me of readReplace and writeResolve in Java serialization.

Original comment by limpbizkit on 1 Oct 2011 at 5:05

GoogleCodeExporter commented 9 years ago
This is relatively straightforward with Gson 2.1's TypeAdapters. @cph, would 
you be interested in contributing a TypeAdapter to Gson's extras/ package?

Original comment by limpbizkit on 29 Dec 2011 at 5:57

GoogleCodeExporter commented 9 years ago

Original comment by limpbizkit on 18 Mar 2012 at 8:18

GoogleCodeExporter commented 9 years ago
This doesn't need to be in core Gson.

Original comment by limpbizkit on 2 Sep 2012 at 9:48