Jackson @Jsonproperty Not Working If Property Name Not Equal Field Name

How to covert JSON field name to a Java compatible property name while doing Jackson de-serialisation?

The issue has been resolved and full credit goes to both sotirios-delimanolis and staxman.Generally people like to find direct answer and not really like to going through the comments although the actual answer can be present among those comments. So this answer is dedicated for those kinds of users who like to find the direct answers.

If you are using Jackson to convert JSON to/from Java object and the Jackson API version is lower than 1.8 then you need to annotate both the getter and setter method with @JsonProperty if JSON field names and Java property names are different.

So, I have annotated both the setter methods with @JsonProperty annotation. Below is the modified code (ServerDetails.java)

@JsonProperty("server-id")
public Integer getServerId() {
return serverId;
}

// added
@JsonProperty("server-id")
public void setServerId(Integer serverId) {
this.serverId = serverId;
}

@JsonProperty("server-url")
public String getServer_url() {
return server_url;
}

// added
@JsonProperty("server-url")
public void setServer_url(String server_url) {
this.server_url = server_url;
}

If you want to avoid this extra effort, then you need to upgrade your jackson-all jar version and the version should be higher than 1.7.

Why? Why higher than 1.7?

In staxman's words

key difference is 1.7 vs 1.8: latter added code to "unify" annotations, so you do not need to add renaming for both getter and setter. With 1.7 you would need to add annotation for both, and your class only had them for one. So, not technically a bug (it was defined behavior), but missing functionality.

Hope this answer will help someone who is facing the same problem.

Jackson Not Overriding Getter with @JsonProperty

The problem was that I was using both the old and new jackson libraries

i.e. before I had
import org.codehaus.jackson.annotate.JsonProperty;
Which I had to change to below, to be consistent with the library I was using.

Since I was using maven that also meant updating my maven dependencies.
import com.fasterxml.jackson.annotation.JsonProperty;

For it to work, I needed the @JsonProperty annotation on the getter (putting it on the object didn't work)

I found the answer here (thanks to francescoforesti)
@JsonProperty not working as expected

Different names of JSON property during serialization and deserialization

Just tested and this works:

public class Coordinates {
byte red;

@JsonProperty("r")
public byte getR() {
return red;
}

@JsonProperty("red")
public void setRed(byte red) {
this.red = red;
}
}

The idea is that method names should be different, so jackson parses it as different fields, not as one field.

Here is test code:

Coordinates c = new Coordinates();
c.setRed((byte) 5);

ObjectMapper mapper = new ObjectMapper();
System.out.println("Serialization: " + mapper.writeValueAsString(c));

Coordinates r = mapper.readValue("{\"red\":25}",Coordinates.class);
System.out.println("Deserialization: " + r.getR());

Result:

Serialization: {"r":5}
Deserialization: 25

JsonIgnore and JsonProperty not behaving as I expect

I tried executing your code with Jackson 2.2 and i'm getting the expected output

import java.util.ArrayList;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.annotation.JsonSerialize.Inclusion;

public class Test {

@JsonSerialize(include = Inclusion.NON_NULL)
public static class Manifest {

public Manifest(){
this.files = new ArrayList<String>();
}

@JsonProperty("manifest-version")
private String manifestVersion;

private ArrayList<String> files;

@JsonIgnore
public String getManifestVersion() {
return manifestVersion;
}

@JsonIgnore
public void setManifestVersion(String manifestVersion) {
this.manifestVersion = manifestVersion;
}

public ArrayList<String> getFiles() {
return files;
}

public void setFiles(ArrayList<String> files) {
this.files = files;
}
public void addFile(String file) {
this.files.add(file);
}

}

public static void main(String[] args) throws JsonProcessingException {
ObjectMapper obj = new ObjectMapper();
Manifest m = new Manifest();
m.setManifestVersion("2.0");
System.out.println(obj.writeValueAsString(m));
}
}

Output: {"files":[],"manifest-version":"2.0"}

what version of jackson are you using?



Related Topics



Leave a reply



Submit