【问题标题】:How to ignore field in runtime when serializing in Jackson在杰克逊中序列化时如何在运行时忽略字段
【发布时间】:2014-07-01 16:40:04
【问题描述】:

我有一个包含很多布尔字段的 bean。我只想将那些具有真实值的字段添加到 json 以保存一些有效负载。这是一项功能,应基于客户的需求,因此必须以动态方式完成。我不认为注释会起作用,因为它们是静态的东西。对此有什么想法吗?

【问题讨论】:

    标签: json spring-mvc serialization jackson


    【解决方案1】:

    类似Jackson's JSON Views?

    在 Spring 的问题跟踪器中有 an issue open for that

    【讨论】:

      【解决方案2】:

      除了杰克逊的观点,你还可以写一个custom Jackson filter,它会过滤掉所有布尔字段的负值。

      这是一个例子:

      public class JacksonFilterBoolean {
          @JsonFilter("boolean-filter")
          public static class Test {
              public final Boolean f1;
              public final boolean f2;
              public final boolean f3;
              public final Boolean fNull = null;
              public final String f4 = "string";
      
              public Test(Boolean f1, boolean f2, boolean f3) {
                  this.f1 = f1;
                  this.f2 = f2;
                  this.f3 = f3;
              }
          }
      
          public static class BooleanPropertyFilter extends SimpleBeanPropertyFilter {
              @Override
              protected boolean include(BeanPropertyWriter writer) {
                  return true;
              }
      
              @Override
              protected boolean include(PropertyWriter writer) {
                  return true;
              }
      
              @Override
              public void serializeAsField(Object pojo, JsonGenerator jgen,
                                           SerializerProvider provider, PropertyWriter writer)
                      throws Exception {
                  if (writer instanceof BeanPropertyWriter) {
                      BeanPropertyWriter bWriter = (BeanPropertyWriter) writer;
                      Class<?> type = bWriter.getType().getRawClass();
                      if (type == Boolean.class || type == boolean.class) {
                          Object o = bWriter.get(pojo);
                          if (o != null && !(boolean) o) {
                              return;
                          }
                      }
                  }
                  super.serializeAsField(pojo, jgen, provider, writer);
              }
          }
      
          public static void main(String[] args) throws JsonProcessingException {
              Test t = new Test(true, false, true);
              ObjectMapper mapper = new ObjectMapper();
              mapper.setFilters(new SimpleFilterProvider().addFilter("boolean-filter",
                      new BooleanPropertyFilter()));
              System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(t));
          }
      
      }
      

      输出:

      {
        "f1" : true,
        "f3" : true,
        "fNull" : null,
        "f4" : "string"
      }
      

      【讨论】:

      • 谢谢。这正是我想要的。
      猜你喜欢
      • 1970-01-01
      • 2021-04-26
      • 1970-01-01
      • 2015-05-18
      • 2017-02-15
      • 2013-03-01
      • 1970-01-01
      • 2014-06-22
      • 1970-01-01
      相关资源
      最近更新 更多