javajsonjackson

How to define optional json field using Jackson


I have an object with one optional field and can not find proper annotation to model it. Any ideas what is the proper way to do it with Jackson?


Solution

  • In Jackson you cannot make the difference between optional and non-optional fields. Just declare any field in your POJO. If a field is not present in your JSON structure then Jackson will not call the setter. You may keep track of whether a setter has been called with a flag in the POJO.