jackson-future-ideas icon indicating copy to clipboard operation
jackson-future-ideas copied to clipboard

set default value if null (or meet some condition) in deserialization

Open wszk1992 opened this issue 5 years ago • 5 comments

I'm wondering if we can have an annotation (field level and class level) to set a default value if the corresponding JSON value is null in deserialization.

{
  "name": "Jack"
}
class People {
  String name;
  @JsonDefault(intDefault = 10)
  Integer age;
}

After deserialization, We can have people.name == "Jack" and people.age == 10.

@JsonDefault(floatDefault = 0.0f, intDefault = 0)
class People {
  String name;
  Integer age;
  Float weight;
}

After deserialization, We can have people.name == "Jack", people.age == 0 and people.weight == 0.0

wszk1992 avatar May 21 '19 15:05 wszk1992