Skip to content

Support for an easier way to deserialize missing values into an empty list? #742

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

Open
seekM opened this issue Mar 27, 2025 · 4 comments
Open

Comments

@seekM
Copy link

seekM commented Mar 27, 2025

Please consider the following scenario:

public record Bar(ArrayList<String> bar) {}

void main() throws JsonProcessingException {
  var bar = new XmlMapper().readValue("<Bar></Bar>", Bar.class);
  System.out.println(bar); // Desired output: Bar[bar=[]]
}

As stated in the comment in above code, I'd like missing values to be deserialized into an empty list. So far I managed to make this possible with these two approaches:

  1. Add a custom constructor to the Bar record which takes care of null and returns an empy list.
  2. Write a custom StdDeserializer for Bar.

I'd like to avoid approach 1) because I'd like to keep the parsing related functionality to annotations and not have it inside the record class. On the other hand, approach 2) requires a bigger setup. I was therefore wondering, is it possible to deserialize missing values into an empty list in an easier way - for example, like deserializing a missing value into an empty Optional is supported by registering the Jdk8Module - and if not, could this maybe be a feature in the future?

@pjfanning pjfanning transferred this issue from FasterXML/jackson-databind Mar 27, 2025
@pjfanning
Copy link
Member

https://stackoverflow.com/questions/32398885/jackson-deserializer-change-null-collection-to-empty-one

May be more answers in stackoverflow or in chatgpt. It really helps if users are willing to check for existing solutions online.

@seekM
Copy link
Author

seekM commented Mar 27, 2025

Annotating the record elment bar with @JsonSetter(nulls = Nulls.AS_EMPTY) does help, thank you!

I indeed did some research and also had already tried the solution above before, but unfortunately, in the following process of simplifying my original code and narrowing down the problem I missed that in my original code I additionally set

xmlMapper.enable(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES);

With this set, the solution above doesn't seem to work, unfortunately. I guess what I would like to have is "fail on missing creator properties which are required, but for all "optional" types like Optional and List use the "empty" instance of this type". If I understand it correctly, this already has been asked for here for Optional? The last comment says that this issue has been replaced by jackson-databind#1402, but I'm not sure if I understand the solution described in this issue / the status of this issue - does what has been implemented there offer a way to achieve what I described above?

@cowtowncoder
Copy link
Member

Quick question: is this with the latest (2.18.3)? If not, please try with it.

@seekM
Copy link
Author

seekM commented Mar 28, 2025

Yes, I used 2.18.3. I just created a repository with example code.

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

3 participants