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

Deserialising an empty character as json null instead of json undefined #26

Open
milanov opened this issue Aug 2, 2021 · 3 comments
Open

Comments

@milanov
Copy link

milanov commented Aug 2, 2021

Recently we changed a type in one object from JsonNullable<String> sth = JsonNullable.undefined() to JsonNullable<Character> sth = JsonNullable.undefined(). Soon after we noticed a change in the deserialisation behaviour - explicit updates of that property to an empty string ("") no longer worked, as they were treated as not present (JsonNullable.undefined instead of JsonNullable[null]).

It seems that the issue is in the JsonNullableDeserializer, namely in the constructor:

this.isStringDeserializer = ((ReferenceType) fullType).getReferencedType().isTypeOrSubTypeOf(String.class);

Does it make sense to change the check and include Character as well as String? Or would you suggest another alternative? If it is the former, we can also open a PR, as the change doesn't seem so large (and there is already #4 for inspiration).

@bratkartoffel
Copy link
Contributor

I think the Problem is, that a Character can only be set or null, there is nothing like an empty value. Furthermore, there is no such json type like a single character. JSON only knows about Boolean, Numbers, Strings, Arrays, Lists and an object consiting of these. A single character is basically just a number. When you deserialize a string value to a character via jackson, it's some kind of implicit / convenience conversion and just works although it's not really defined by the json standard.

I think what you would like to achieve can only be made by using JsonNullable<Integer> with:

  • 0 == empty()
  • null = undefined()
  • any other

If you need an char, you can just cast the integer prior using it.

@bratkartoffel
Copy link
Contributor

After thinking about this issue, I think it's best to align the deserialization behaviour for String and Character to make it more consistent. See PR #45.

@wing328
Copy link
Member

wing328 commented Feb 13, 2023

@milanov can you please test the fix filed by @bratkartoffel to see if it works for you?

#45

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

Successfully merging a pull request may close this issue.

3 participants