sinamdar2015 / google-gson

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

Allow ExclusionStrategy by field value. #459

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
When implementing the ExclusionStrategy, in shouldSkip* methods we have access 
to the fields attributes but not the fields values.

Might be usefull to be able to filter based on some specific values. So the 
idea here is to enhance the ExclusionStrategy to pass not just FieldAttributes 
but also the source object itself.

Original issue reported on code.google.com by jean-m...@spaggiari.org on 3 Jul 2012 at 6:30

GoogleCodeExporter commented 9 years ago
I'm also interested in this and as I understand from
https://groups.google.com/forum/#!topic/google-gson/pk2vAcUk5rQ
you also need a special case only: exclusion of the default value. This can be 
achieved via the patch I've attached to the discussion. Use at own risk...

Original comment by Maaarti...@gmail.com on 1 Aug 2012 at 3:33

GoogleCodeExporter commented 9 years ago
I got your patch. I will give it a try this week because this is exactly what 
I'm looking for and was trying to achieve. I will provide feedback of my tests 
on the distribution list.

Original comment by jean-m...@spaggiari.org on 1 Aug 2012 at 3:38

GoogleCodeExporter commented 9 years ago
You can also create a TypeAdapter that writes 'null' for the excluded value. 
Gson won't serialize nulls by default.

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

GoogleCodeExporter commented 9 years ago
Sure I can, but...

1. This means to write a TypeAdapter for each such class.
2. Each of these adapters must handle all fields, not only those where the 
default value is to be excluded, right?
3. Each of these adapters must know the default value.

This may be an arbitrary amount of work. Additionally
4. serializeNulls must be turned off, but this is a global setting possibly 
needed for something somewhere else.

Original comment by Maaarti...@gmail.com on 25 Sep 2012 at 12:41

GoogleCodeExporter commented 9 years ago
Maaartin sent a patch on the distribution list for this. Any chance to get that 
integrated on the next release?

Original comment by jean-m...@spaggiari.org on 22 Nov 2012 at 12:46

GoogleCodeExporter commented 9 years ago
+1 for Maaartins patch [1]
I think it's the right way to go and the explaination [2] is very clear.
In my case I cannot use the patch because there is no good way to redistribute 
my app with the dependency to the patch and gson. Except someone redeploys gson 
somewhere as a new library with the patch included.
Until that time I have to implement a TypeAdapter for every JsonElement type 
[3].

[1] - https://groups.google.com/d/msg/google-gson/pk2vAcUk5rQ/_gpcau0adxsJ
[2] - https://groups.google.com/d/msg/google-gson/pk2vAcUk5rQ/SUCK2C4DY10J
[3] - 
http://stackoverflow.com/questions/13120354/excluding-certain-fields-from-serial
ization-based-on-value-in-gson

Original comment by sandro.b...@gmail.com on 23 Jun 2013 at 1:49

GoogleCodeExporter commented 9 years ago
Is patch [2] as linked in comment #6 still need with patch [1], or does is 
supersede it? In any case, could we please finally get this included in an 
official release? And do not forget to include the OmitDefaultValueTest linked 
in [2] :-)

Original comment by sschuberth on 8 May 2014 at 10:28

GoogleCodeExporter commented 9 years ago
Both. The first one implements the annotation and the logic, while the second 
one adds the global setting GsonBuilder.omitDefaultValue() and allows to switch 
it off locally via @OmitDefaultValue(false). It could use more tests, but given 
that it's not gonna get included, I'm not motivated.

Original comment by Maaarti...@gmail.com on 13 May 2014 at 1:03

GoogleCodeExporter commented 9 years ago
I did not yet see a definite comment from any project owner whether your 
patches will be included or not. Do you have a link to a mailing list post 
saying so? Anyway, this issue is still open (and not rejected), so I suppose 
there still is a chance to get your patches (maybe slightly reworked) in.

Original comment by sschuberth on 13 May 2014 at 11:08

GoogleCodeExporter commented 9 years ago
To me (nearly) two years of silence sound loud and clear.

I guess the project owners are busy elsewhere and Gson works good enough for 
them. There isn't much happening here in general.

Original comment by Maaarti...@gmail.com on 13 May 2014 at 3:23