Jackson: Duplicate Boolean fields in JSON.

I would like to discuss different names of JSON property during serialization and deserialization

Issues

I have the boolean field as

private boolean isCustom;

having getter and setters as

public boolean isCustom() {
return isCustom;
}
public void setCustom(boolean isCustom) {
this.isCustom = isCustom;
}

And in this case, my JSON will be

but I want JSON to be

so I added @JsonProperty :

@JsonProperty
private boolean isCustom;

But now there is another problem as my JSON is

Solution

The issue above is because the Jackson serialize now it will use the get, set, and is of the setter and getter.

In the above case, it will use

The best way to implement the above scenario is as follows.

@JsonProperty("isCustom")
private boolean custom;
public boolean isCustom() {
return isCustom;
}
public void setCustom(boolean isCustom) {
this.isCustom = isCustom;
}

In the above case, the JSON will be created as

By default, Jackson maps the fields of a JSON object to fields in a Java object by matching the names of the JSON field to the getter and setter methods in the Java object. Jackson removes the “get” and “set” part of the names of the getter and setter methods and converts the first character of the remaining name to lowercase.

For instance, the JSON field named matches the Java getter and setter methods called and . The JSON field named would match the getter and setter named and .

If you need to match JSON object fields to Java object fields in a different way, you need to either use a custom serializer and deserializer or use some of the many Jackson Annotations.

Java 8 | Spring | React | Angular | CI/CD | Microservices | Monitoring |Apache Kafka. https://www.linkedin.com/in/shuaib-kunhabdulla-83780188