【问题标题】:Make Jackson serializer override specific ignored fields让 Jackson 序列化程序覆盖特定的忽略字段
【发布时间】:2019-11-08 08:55:22
【问题描述】:

我有这样的杰克逊注释类:

public class MyClass {
   String field1;

   @JsonIgnore
   String field2;

   String field3;

   @JsonIgnore
   String field4;
}

假设我无法更改 MyClass 代码。那么,如何让 ObjectMapper 仅覆盖 field2 的 JsonIgnore 并将其序列化为 json ?我希望它忽略 field4。这么简单,几行代码?

我的常规序列化代码:

public String toJson(SomeObject obj){
    ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
    String json = null;
    try {
        json = ow.writeValueAsString(obj);
    } catch (JsonProcessingException e) {
        e.printStackTrace();
    }
    return json;
}

【问题讨论】:

    标签: java json jackson


    【解决方案1】:

    您可以使用MixIn 功能:

    import com.fasterxml.jackson.annotation.JsonIgnore;
    import com.fasterxml.jackson.annotation.JsonProperty;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.fasterxml.jackson.databind.SerializationFeature;
    
    public class JsonApp {
    
        public static void main(String[] args) throws Exception {
            ObjectMapper mapper = new ObjectMapper();
            mapper.enable(SerializationFeature.INDENT_OUTPUT);
            mapper.addMixIn(MyClass.class, MyClassMixIn.class);
    
            System.out.println(mapper.writeValueAsString(new MyClass()));
        }
    }
    
    interface MyClassMixIn {
    
        @JsonProperty
        String getField2();
    }
    

    上面的代码打印:

    {
      "field1" : "F1",
      "field2" : "F2",
      "field3" : "F3"
    }
    

    【讨论】:

    • 谢谢。在mixin中,我们如何指定字段名而不是getter?
    • @BoratSagdiyev,试试@JsonProperty("anotherName")
    【解决方案2】:

    您可以像这样使用自定义序列化程序(我将重构留给您):

    private String getJson(final MyClass obj) {
        final ObjectMapper om = new ObjectMapper();
        om.registerModule(new SimpleModule(
                "CustomModule",
                Version.unknownVersion(),
                Collections.emptyMap(),
                Collections.singletonList(new StdSerializer<MyClass>(MyClass.class) {
    
                    @Override
                    public void serialize(final MyClass value, final JsonGenerator jgen, final SerializerProvider provider) throws IOException {
                        jgen.writeStartObject();
                        jgen.writeStringField("field1", value.field1);
                        jgen.writeStringField("field2", value.field2);
                        jgen.writeStringField("field3", value.field3);
                        jgen.writeEndObject();
                    }
    
                })));
    
        try {
            return om.writeValueAsString(obj);
        } catch (final JsonProcessingException e) {
            throw new RuntimeException(e);
        }
    }
    

    您可以在 google/stackoverflow 上找到有关自定义序列化程序的更多信息,我想我回答了类似的问题 here

    【讨论】:

      猜你喜欢
      • 2012-02-01
      • 2013-03-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-29
      • 2018-07-21
      • 2020-02-16
      • 2019-10-22
      • 2015-07-11
      相关资源
      最近更新 更多