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
You can use a combination of @JsonSetter, and @JsonGetter to control the deserialization, and serialization of your property, respectively. This will also allow you to keep standardized getter and setter method names that correspond to your actual field name.
import com.fasterxml.jackson.annotation.JsonSetter; import com.fasterxml.jackson.annotation.JsonGetter;class Coordinates { private int red; //# Used during serialization @JsonGetter("r") public int getRed() { return red; } //# Used during deserialization @JsonSetter("red") public void setRed(int red) { this.red = red; }}