Java Gson Exclude fields during serialization

前端 未结 7 1610
走了就别回头了
走了就别回头了 2020-12-10 01:48

I have a ConfigInstance class which contains a password and a password_hash. Now I want to serialize the object using gson but exclude

相关标签:
7条回答
  • 2020-12-10 01:57

    If you like particular field not be serialized give it a transient keyword

    private transient String database_pass;
    

    visit https://sites.google.com/site/gson/gson-user-guide#TOC-Finer-Points-with-Objects for more information

    0 讨论(0)
  • 2020-12-10 02:01

    In order to get this result, you need to annotate all the fields with the @Expose:

    public class ConfigInstance {
    
        @Expose
        public String database_address;
        @Expose
        public int database_port;
        @Expose
        public String database_user;
    
        @Expose(serialize = false)
        private String database_pass;
        @Expose
        public String database_pass_hash;
    

    And configure Gson to only expose fields that are annotated and ignore the rest as shown in the following:

    Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
    

    Then, you'll get:

    {
      "database_address": "127.0.0.1",
      "database_port": 1521,
      "database_user": "test",
      "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"
    }
    

    Also, when deserialising the string you'll still have the password attribute as well.


    Still, you have the possibility to configure a Gson Serializer to accomplish this.

    0 讨论(0)
  • 2020-12-10 02:06

    If you want to disable only serialization or only deserialization, you can play with @Expose annotation's attributes, e.g.:

    @Expose(serialize = false, deserialize = true)
    

    Default option is true, so deserialize is unnecessary here.

    0 讨论(0)
  • 2020-12-10 02:09

    @utkusonmez This answer works albeit the method mentioned is wrong. It should be using 'addSerializationExclusionStrategy' instead of 'addDeserializationExclusionStrategy'

    So the answer would look like

    Gson gson = new GsonBuilder()
                .addSerializationExclusionStrategy(new ExclusionStrategy() {
                    @Override
                    public boolean shouldSkipField(FieldAttributes f) {
                        return f.getName().toLowerCase().contains("fieldName");
                    }
    
                    @Override
                    public boolean shouldSkipClass(Class<?> aClass) {
                        return false;
                    }
                })
                .create();
    
    gson.toJson(*OBJ_TO_SERIALIZE*))
    
    0 讨论(0)
  • 2020-12-10 02:09

    Here's a very simple solution with just one line of code, without any @Expose annotation or exclusion strategy.

    The default behaviour that is implemented in Gson is that null object fields are ignored. So, all you need to do is set the password field to null before serializing.

    map.setPassword(null); // or map.password = null;
    String jsonConfig = gson.toJson(map); // This won't serialize null password field
    
    0 讨论(0)
  • 2020-12-10 02:16

    It's a late answer but it may help someone.

    You only have to do the @Expose(serialize = false, deserialize = false) or just what you want.

    If you have serialize and deserialize true its not nesseccary to have @Expose at all.

    Create this Class:

    import com.google.gson.ExclusionStrategy;
    import com.google.gson.FieldAttributes;
    import com.google.gson.annotations.Expose;
    
    public class NoModuleExclusionStrategy implements ExclusionStrategy {
    
        private final boolean deserialize;
    
        public NoModuleExclusionStrategy(boolean isdeserialize) {
            deserialize = isdeserialize;
        }
    
        @Override
        public boolean shouldSkipClass(Class<?> clazz) {
            return false;
        }
    
        @Override
        public boolean shouldSkipField(FieldAttributes field) {
            return !(field.getAnnotation(Expose.class) == null || (deserialize ? field.getAnnotation(Expose.class).deserialize() : field.getAnnotation(Expose.class).serialize()));
        }
    
    }
    

    and then build Gson with GsonBuilder

    Gson gson = new GsonBuilder()
    .addSerializationExclusionStrategy(new NoModuleExclusionStrategy(false))
    .addDeserializationExclusionStrategy(new NoModuleExclusionStrategy(true))
    .create();
    

    Your ConfigInstance Class will look like follows

    {
      "database_address": "127.0.0.1",
      "database_port": 1521,
      "database_user": "test",
      "database_pass_hash": "B9FE2C011B59F0D0D383D70073E48A19"
    }
    
    0 讨论(0)
提交回复
热议问题