I have a field in the object:

@SerializedName("CommentPhotoes") @Expose(serialize = false) private ArrayList<CommentPhoto> commentPhotos; 

I need to parse this field when I read this object from a string, but when I serialize it back into a string, this field should not go there.

The problem is that when you serialize an object, this field is also serialized. What has the same applies to deserialization.

What could be the problem, can I misuse these annotations?

    2 answers 2

    By default, the presence of the @Expose annotation does not affect anything. Therefore, when creating a Gson object using GsonBuilder you need to explicitly specify how to handle these annotations.

    Possible options:

    1. Mark required fields with this annotation
    2. Mark ignored fields with this annotation
    3. Use the transient modifier instead of annotations

    More details:

    1. Mark the fields that we want to serialize / deserialize with the @Expose annotation, when creating the Gson object we say that the fields without this annotation should be ignored:

       Gson gson = new GsonBuilder() .excludeFieldsWithoutExposeAnnotation() .create(); 
    2. Mark the fields that we want to ignore both in serialization and deserialization with the @Expose annotation, when creating a Gson object, add ExclusionStrategies :

       Gson gson = new GsonBuilder() .setExclusionStrategies(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { return f.getAnnotation(Exclude.class) != null; } @Override public boolean shouldSkipClass(Class<?> c) { return false; } }) .create(); 

      To exclude fields only in serialization or only in deserialization, you must add the @Expose annotation with the parameter serialize=false or deserialize=false :

       class Book { // игнорируется только при сериализации @Expose(serialize = false) String field1; // игнорируется только при десериализации @Expose(deserialize = false) String field2; } 

      and when creating a Gson object, configure the correct exclusion of fields with these annotations:

       new GsonBuilder() .addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { Expose expose = f.getAnnotation(Expose.class); return expose != null && !expose.serialize(); } @Override public boolean shouldSkipClass(Class<?> c) { return false; } }) .addDeserializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes f) { Expose expose = f.getAnnotation(Expose.class); return expose != null && !expose.deserialize(); } @Override public boolean shouldSkipClass(Class<?> c) { return false; } }) .create(); 
    3. You can also add a transient modifier to the field to exclude it:

       class Book { transient String name; } 
    • one
      Thanks for the detailed answer! - Kirill Stoianov

    You may not have configured an instance of Gson . Try this :

     new GsonBuilder() .addSerializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes fieldAttributes) { final Expose expose = fieldAttributes.getAnnotation(Expose.class); return expose != null && !expose.serialize(); } @Override public boolean shouldSkipClass(Class<?> aClass) { return false; } }) .addDeserializationExclusionStrategy(new ExclusionStrategy() { @Override public boolean shouldSkipField(FieldAttributes fieldAttributes) { final Expose expose = fieldAttributes.getAnnotation(Expose.class); return expose != null && !expose.deserialize(); } @Override public boolean shouldSkipClass(Class<?> aClass) { return false; } }) .create(); 
    • Thanks for the answer, the working code, but I have to accept the answer diraria) - Kirill Stoianov
    • @KirillStoianov, yes not a question) Another answer is much better) - Yuriyi