Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting default values to null fields when mapping with Jackson

Tags:

java

json

jackson

I am trying to map some JSON objects to Java objects with Jackson. Some of the fields in the JSON object are mandatory(which I can mark with @NotNull) and some are optional.

After the mapping with Jackson, all the fields that are not set in the JSON object will have a null value in Java. Is there a similar annotation to @NotNull that can tell Jackson to set a default value to a Java class member, in case it is null?

Edit: To make the question more clear here is some code example.

The Java object:

class JavaObject {     @NotNull     public String notNullMember;      @DefaultValue("Value")     public String optionalMember; } 

The JSON object can be either:

{     "notNullMember" : "notNull" } 

or:

{     "notNullMember" : "notNull",     "optionalMember" : "optional" } 

The @DefaultValue annotations is just to show what I am asking. It's not a real annotation. If the JSON object is like in the first example I want the value of the optionalMember to be "Value" and not null. Is there an annotation that does such a thing?

like image 809
gookman Avatar asked Sep 14 '13 19:09

gookman


People also ask

How do you tell Jackson to ignore a field during serialization if its null?

You can ignore null fields at the class level by using @JsonInclude(Include. NON_NULL) to only include non-null fields, thus excluding any attribute whose value is null. You can also use the same annotation at the field level to instruct Jackson to ignore that field while converting Java object to json if it's null.

How do I ignore properties in ObjectMapper?

ObjectMapper; ObjectMapper objectMapper = new ObjectMapper(); objectMapper. configure(DeserializationFeature. FAIL_ON_UNKNOWN_PROPERTIES, false); This will now ignore unknown properties for any JSON it's going to parse, You should only use this option if you can't annotate a class with @JsonIgnoreProperties annotation.

How do I ignore JsonProperty?

To ignore individual properties, use the [JsonIgnore] attribute. You can specify conditional exclusion by setting the [JsonIgnore] attribute's Condition property. The JsonIgnoreCondition enum provides the following options: Always - The property is always ignored.


2 Answers

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"; } 
like image 92
Ilya Avatar answered Sep 18 '22 08:09

Ilya


Only one proposed solution keeps the default-value when some-value:null was set explicitly (POJO readability is lost there and it's clumsy)

Here's how one can keep the default-value and never set it to null

@JsonProperty("some-value") public String someValue = "default-value";  @JsonSetter("some-value") public void setSomeValue(String s) {     if (s != null) {          someValue = s;      } } 
like image 24
Sergei Voitovich Avatar answered Sep 21 '22 08:09

Sergei Voitovich