Skip to content

Confusion with setSerializationInclusion(Include.NON_NULL) #55

@midovlvn

Description

@midovlvn

I'm confused with setup in "Usage section":


ObjectMapper mapper = new ObjectMapper();
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
mapper.registerModule(new JsonNullableModule());

Please take a look on the following snippet ( val=null is passed in JSON and in Map):

record A (JsonNullable<BigDecimal> val) {}
ObjectMapper objectMapper = new ObjectMapper().registerModule(new JsonNullableModule());

A a1 = objectMapper.readValue("{ \"val\" : null }", A.class);
System.out.println(a1);

HashMap hashMap = new HashMap();
hashMap.put("val", null);
A a2 = objectMapper.convertValue(hashMap, A.class);
System.out.println(a2);

Output is consistent

A[val=JsonNullable[null]]
A[val=JsonNullable[null]]

But with setup

ObjectMapper objectMapper = new ObjectMapper().registerModule(new JsonNullableModule())
        .setSerializationInclusion(JsonInclude.Include.NON_NULL);

Output is not consistent

A[val=JsonNullable[null]]
A[val=JsonNullable.undefined]

Well, time for questions:

  1. I do not understand why you propose in "Usage" section setup .setSerializationInclusion(JsonInclude.Include.NON_NULL). It looks useless.
  2. I do not understand how serialization setup may affect deserialization (objectMapper.convertValue is deserialization)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions