shau-lok / google-gson

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

Allow providing a custom visitor to preprocess or postprocess default serialization / deserialization #315

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
I have the following use cases:

1. When searching a list of, for example, products I want to be able to (only 
in this situation) provide a way to only serialize fields that the client asks 
for. I could use a DTO to do the job but, in my use-case (multiple lists with 
custom fields) it would be very unnatural. I could simply use the exclusion 
strategy that GSON provides but in this use case it is just not enough. Imagine 
one of the fields the client asks for is, for example, product.country.name. 
Then, if I want a generic way to serialize this I need to know the precise 
context of the field being serialized (I mean context as the stack of objects 
already visited in that branch of the object graph).

2. When posting a complex form (multiple associations through html select 
fields) I only want to send the id of the select form fields instead of the 
whole object graph (avoiding a lot of traffic). The problem is that if I need 
to rebuild the object when it arrives to the server I need to provide a custom 
serializer / deserializer for doing this. With a custom visitor I could 
post-process these kind of objects in some generic way (for example during 
deserialization).

These two use-cases (I hope I have explained myself well) has brought me to 
develop an extension of GSON to allow to provide a custom JsonVisitor through a 
factory (to avoid reflection and the derived performance gain) configured in 
GSON builder. Attached you can find a patch for v1.7.1 (tag gson-1.7.1) with a 
unit test case.

Instead of using a single custom gson visitor (as for example in the Exclusion 
Strategies) I wanted a factory so I don't have to deal with thread locals in my 
implementations to store the object stack being processed. 

Any comments are very welcome.

Original issue reported on code.google.com by ignacio....@kotasoft.com on 18 Apr 2011 at 1:54

Attachments:

GoogleCodeExporter commented 9 years ago
Sorry for posting this as a Defect. Obviestly this is an enhancement... Is 
there anyway to change it or should I file a new issue?

Original comment by ignacio....@kotasoft.com on 18 Apr 2011 at 1:55

GoogleCodeExporter commented 9 years ago
Issue #20 (I have seen it has been accepted) could be relatively easily solved 
with this patch! 

Original comment by ignacio....@kotasoft.com on 18 Apr 2011 at 2:21

GoogleCodeExporter commented 9 years ago
This fix (over the previous patch) solves a NPE issue on Gson and probably has 
a minimal performance gain over previous patch.

Original comment by ignacio....@kotasoft.com on 18 Apr 2011 at 2:46

Attachments:

GoogleCodeExporter commented 9 years ago
Issue 144 has been merged into this issue.

Original comment by joel.leitch@gmail.com on 19 Apr 2011 at 9:52

GoogleCodeExporter commented 9 years ago
Test for issue 20 using this patch

Original comment by ignacio....@kotasoft.com on 21 Apr 2011 at 12:58

Attachments:

GoogleCodeExporter commented 9 years ago
You can do this in Gson 2.1 by writing your own TypeAdapterFactory. Look at 
ReflectiveTypeAdapterFactory if you need inspiration.

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