Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How set jackson for ignore a boolean property when it false?

Tags:

java

jackson

I use Jackson, ObjectMapper.readValue(json, Class).

Have a class:

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

and extensible class:

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

and Complex class:

ComplexComponent extends Component {
      ExtendedComponent component;
    }

and 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"
        }
      ]
    }
  }
}

How to get objects only with name someName1 and someName3 in ExtendedComponent ?

And how to get nullable ComplexComponent if all of components is not showed?

like image 719
Артем Матюхин Avatar asked Jan 29 '23 00:01

Артем Матюхин


1 Answers

Depending on your version of jackson 2

@JsonInclude(Include.NON_DEFAULT)

or

@JsonInclude(Include.NON_EMPTY)

should do the trick.

like image 72
TomWolk Avatar answered Jan 30 '23 13:01

TomWolk