paul-stinchcombe / 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

GoogleCodeExporter commented 9 years ago
I'd like a strategy like in comment #1 be implemented inside library along with 
the annotation for that. It can be configurable on GsonBuilder, but should be 
part of the library.

That's good it is easy to do it on my own, but purpose of using library is not 
to write the stuff myself.

Original comment by tomasz.b...@pragmatists.pl on 2 Jul 2012 at 2:35