discomarathon / google-gson

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

Unwanted side-effects when using transient #316

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. mark a field you want to store in DB but not serialize with gson as transient
2. use the class with a Hibernate-like framework

What is the expected output? What do you see instead?
  I would like to be easily able to exclude a field from (de)serialization with gson, but the transient modifier is used by some frameworks, and therefore my field cannot be stored…

What version of the product are you using? On what operating system?
Gson 1.6

Please provide any additional information below.
I attached a patch that adds a skipFieldsWithExcludeAnnotation method like for 
the Expose annotation.

Why not use Expose ? Because it is faster to @Exclude one field than to @Expose 
all the others.

Original issue reported on code.google.com by julien.d...@clever-cloud.com on 18 Apr 2011 at 10:13

Attachments:

GoogleCodeExporter commented 9 years ago
I don't think we want to support this in Gson directly. I think @Expose was a 
mistake, but now we are stuck with it :-). This is really easy for you to do 
yourself if you are using Gson 1.7 or later.

Instead, what I recommend is that you write your own Exclusion Strategies for 
serialization and deserialization. Then you would create your Gson instance as 
follows:
Gson gson = new GsonBuilder()
    .addSerializationExclusionStrategy(new MySerializationExclusionStrategy())
    .addDeserializationExclusionStrategy(new MyDeserializationExclustionStrategy())
    .create();

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

GoogleCodeExporter commented 9 years ago
Yeah, I (we) already use that, but we thought we could help a little.

Anyway, if you suddenly want to add it in gson, the patch is here ;-).

Original comment by julien.d...@clever-cloud.com on 20 Apr 2011 at 7:15