Skip to content

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

Open
@seekM

Description

@seekM

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?

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