dungnn / google-gson

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

Easier way to exclude properties #450

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Suggested way to exclude property from serializing/deserializing is the @Expose 
annotation, but when option 
GsonBuilder().excludeFieldsWithoutExposeAnnotation() is used I need to use 
@Expose on all fields I intend to serialize.

This is very annoying provided I have a big graph of data and only want to 
exclude one property.

Suggested way is to have an annotation @ExcludeFromGson that would always be 
read and properties marked with this annotation would not be 
serialized/deserialized.

Original issue reported on code.google.com by tomasz.b...@pragmatists.pl on 8 Jun 2012 at 12:41

GoogleCodeExporter commented 9 years ago
public class YourAnnotationExclusionStrategy implements ExclusionStrategy {
    public boolean shouldSkipClass(Class<?> clazz) {return false;}
    public boolean shouldSkipField(FieldAttributes f) {return f.getAnnotation(YourAnnotation.class) != null;}
}

GsonBuilder().setExclusionStrategies(new YourAnnotationExclusionStrategy(), ...

Original comment by spdenne on 13 Jun 2012 at 8:36

GoogleCodeExporter commented 9 years ago
Let me know if there's action we should be taking here.

Original comment by limpbizkit on 30 Jun 2012 at 3:09