如何设置jackson在忽略布尔属性时为false?

时间:2018-06-02 08:35:28

标签: java jackson

我使用Jackson,ObjectMapper.readValue(json,Class)。

上课:

Component {
  private String name;
  private String someField;
  private boolean show = true; // if false -> skip it object
}

和可扩展的类:

ExtendedComponent extends Component {
  private List<Component> components = emptyList();// if all of object in list not showed -> skip field
}

和复杂类:

ComplexComponent extends Component {
      ExtendedComponent component;
    }

和json:

{
  "complexComponent": {
    "name": "complexName1",
    "show": true,
    "someField": "complex",
    "extendedComponent": {
      "components": [
        {
          "name": "someName1",
          "show": true,
          "someField": "someField"
        },
        {
          "name": "someName2",
          "show": false,
          "someField": "someField"
        },
        {
          "name": "someName3",
          "show": true,
          "someField": "someField"
        }
      ]
    }
  }
}

如何在ExtendedComponent中只获取名称为someName1和someName3的对象?

如果没有显示所有组件,如何获得可以为空的ComplexComponent?

1 个答案:

答案 0 :(得分:2)

取决于您的杰克逊2版本

    @JsonInclude(Include.NON_DEFAULT)

    @JsonInclude(Include.NON_EMPTY)

应该可以解决问题。