11

I am building JSON from Java object tree using Jackson ObjectMapper. Some of my Java objects are collections and sometimes they might be empty. So if they are empty that ObjectMapper generates me: "attributes": [], and I want to exclude those kind of empty JSON arrays from my result. My current ObjectMapper config:

SerializationConfig config = objectMapper.getSerializationConfig();
config.setSerializationInclusion(JsonSerialize.Inclusion.NON_NULL);
config.set(SerializationConfig.Feature.WRAP_ROOT_VALUE, true);

From this post I've read that I can use:

config.setSerializationInclusion(JsonSerialize.Inclusion.NON_DEFAULT);

But that is generating me an error:

Caused by: java.lang.IllegalArgumentException: Class com.mycomp.assessments.evaluation.EvaluationImpl$1 has no default constructor; can not instantiate default bean value to support 'properties=JsonSerialize.Inclusion.NON_DEFAULT' annotation.

So how should I prevent those empty arrays to appear in my result?

Community
  • 1
  • 1

3 Answers3

19

You should use:

config.setSerializationInclusion(JsonSerialize.Inclusion.NON_EMPTY);

for Jackson 1 or

config.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);

for Jackson 2

  • I am using Jackson: 1.8.5, but there is not such option: JsonSerialize.Includsion.NON_EMPTY. There is only: ALWAYS, NON_NULL, NON_DEFAULT –  Feb 06 '13 at 10:24
  • Fixed a type (Inclusion rather than Incudsion), but you need at least Jackson 1.9 to use it I'm afraid. –  Feb 06 '13 at 10:26
  • Thanks, I've updated the version of Jackson and thats fine now :) –  Feb 06 '13 at 10:34
2

A very good example describing :

  • JsonInclude.Include.NON_NULL
  • JsonInclude.Include.ABSENT
  • JsonInclude.Include.NON_EMPTY

In : https://www.logicbig.com/tutorials/misc/jackson/json-include-non-empty.html

@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class Employee {
  private String name;
  private String dept;
  private String address;
  private List<String> phones;
  private AtomicReference<BigDecimal> salary;
    .............
}

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      Employee employee = new Employee();
      employee.setName("Trish");
      employee.setDept("");
      employee.setAddress(null);
      employee.setPhones(new ArrayList<>());
      employee.setSalary(new AtomicReference<>());

      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writeValueAsString(employee);
      System.out.println(jsonString);
  }
}

=====> Result :

If we don't use @JsonInclude annotation at all then output of the above example will be: {"name":"Trish","dept":"","address":null,"phones":[],"salary":null}

If we use @JsonInclude(JsonInclude.Include.NON_NULL) on Employee class then output will be: {"name":"Trish","dept":"","phones":[],"salary":null}

If we use @JsonInclude(JsonInclude.Include.NON_ABSENT) then output will be: {"name":"Trish","dept":"","phones":[]}

If we use @JsonInclude(JsonInclude.Include.NON_EMPTY) : {"name":"Trish"}

eisnard
  • 41
  • 4
1

If you can modify the object to be serialized, you can also place an annotation directly on the field, for example (Jackson 2.11.2):

@JsonProperty
@JsonInclude(JsonInclude.Include.NON_EMPTY)
private Set<String> mySet = new HashSet<>();

In this way, no further configuration of the ObjectMapper is required.

barfuin
  • 16,865
  • 10
  • 85
  • 132