Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusion with setSerializationInclusion(Include.NON_NULL) #55

Open
midovlvn opened this issue Sep 21, 2023 · 0 comments
Open

Confusion with setSerializationInclusion(Include.NON_NULL) #55

midovlvn opened this issue Sep 21, 2023 · 0 comments

Comments

@midovlvn
Copy link

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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant