Setting Default Values to Null Fields When Mapping With Jackson

Setting default values to null fields when mapping with Jackson

There is no annotation to set default value.

You can set default value only on java class level:

public class JavaObject 
{
public String notNullMember;

public String optionalMember = "Value";
}

java jackson set default value

We can make the JSON setter like the below:

@JsonSetter("CountryCode")
public void setCountryCode (String countryCode) {
if (!countryCode.equals(null) && !countryCode.equals("")) {
this.countryCode = countryCode;
}
}

I think it will serve your requirement. If not work, let us know the details.

Ref: reference

How do you specify default values for Jackson deserialization

You can define a custom getter property where setting your default value in case of null.

   public Integer getAge() {
if (age == null) {
return 18;
} else {
return this.age;
}
}

Note that you can't change the setAge method because it is not invoked in this case, infact there is no age field that will inform Jackson to call that method.


An alternative is to use a custom constructor and use the JsonSetter annotation with the value Nulls.SKIP

Value that indicates that an input null value should be skipped and no assignment is to be made; this usually means that the property will have its default value.

as follow:

 public class User {
@JsonSetter(nulls = Nulls.SKIP)
private Integer age;

public User() {
this.age = 18;
}

...
}

The @JsonSetter is present in package com.fasterxml.jackson.annotation and can be imported as dependency in maven using

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>YOURVERSION</version>
</dependency>

Jackson 2.11.4: Set Default Value For Missing JSON field

You could just get rid of the args constructor and default the value to "Yes". If value is explicitly set to null or to a value it will be assigned. In case of missing value it will be defaulted.

@Getter
@ToString
@Setter
@NoArgsConstructor
public class Choices {
@JsonProperty("choiceId")
@NonNull
private String choiceId;

@JsonProperty("choiceType")
private String choiceType = "Yes";

}

Reference - Jackson: What happens if a property is missing?

jackson assign default value of a property is missing

You need not do anything for this,

class Student {
public String course = "MATH";
}

If course value is missing from the JSON, the value in the bean will by default set to MATH.

Jackson deserialize default values missing

@AllArgsConstructor creates the following constructor

@ConstructorProperties({"name", "values"})
ExampleDto(String name, List<String> values) {
this.name = name;
this.values = values;
}

The constructor is annotated with @ConstructorProperties which means a property-based creator (argument-taking constructor or factory method) is available to instantiate values from JSON object so jackson-databind uses this constructor to instantiate an object from ExampleDto class.

When the following line executes

mapper.readValue("{\"name\": \"Foo\"}", ExampleDto.class);

because there's no value for values in the provided JSON, null is passed for the second argument when the constructor is invoked.

If you remove @AllArgsConstructor annotation jackson-databind would use setter methods to initialize the object and in this case values would not be null

give a default value for an attribute if the value is null in json by jackson

public class Student {
private Integer x = Integer.valueOf(1000);

public Integer getX() {
return x;
}

public void setX(Integer x) {
if(x != null) {
this.x = x;
}
}
}

This works for me........

Test code 1:

public static void main(String[] args) throws IOException {
String s = "{\"x\":null}";
ObjectMapper mapper = new ObjectMapper();
Student ss = mapper.readValue(s, Student.class);
System.out.println(ss.getX());
}

output:

1000

Test code 2:

public static void main(String[] args) throws IOException {
String s = "{}";
ObjectMapper mapper = new ObjectMapper();
Student ss = mapper.readValue(s, Student.class);
System.out.println(ss.getX());
}

output:

1000

How to control null deserialization with default values and required values in Jackson with Kotlin?

Updating the Kotlin module to version 2.9.7 produces an exception when firstProperty is missing. I've tested it repeatedly, going back and forth between 2.9.6 and 2.9.7 with the code and configuration copied from the OP.

A bug fix in 2.9.7 matches the unexpected behavior described in the OP.

Fixes #168 where JsonProperty(required=true) was being ignored and overridden by nullability check

https://github.com/FasterXML/jackson-module-kotlin/issues/168



Related Topics



Leave a reply



Submit