When I first tried out your SomeData
class and serialized it I got the following results:
{"tnAvailable":true,"sTempChange":"trial_000","stempChange":"trial_000"}
This means that jackson doesn't match your getters/setters with the sTempChange property and they are treated as different properties. After adding the following configuration for my mapper I was able to reproduce your case:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.ANY);
objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.ANY);
Now the reason for your error is because Jackson uses its own implementation of bean utilities (com.fasterxml.jackson.databind.util.BeanUtil
) which is used when a class is processed for fields, getters and setters (done by com.fasterxml.jackson.databind.introspect.POJOPropertiesCollector
) when an instance is serialized/deserialized. Methods of interests are okNameForGetter
and okNameForSetter
. In those methods there are 2 other methods used depending on the MapperFeature.USE_STD_BEAN_NAMING
(it is passed in the stdNaming
argument in all methods). The two methods are used in the following manner:
return stdNaming
? stdManglePropertyName(name, prefix.length())
: legacyManglePropertyName(name, prefix.length());
The stdManglePropertyName
follows the Java Beans specification in section 8.8 and the legacyManglePropertyName
does not and is used in versions prior to 2.5 of Jackson.
Now after running your getter and setter method names through this methods, however you set MapperFeature.USE_STD_BEAN_NAMING
, your getter/setter for sTempChange
property is wrongly named. It should be getsTempChange
(lowercase 's') and getsTempChange
(again lowercase 's') to correctly serialize and deserialize the instances of SomeData
class.
Finally here is some code for testing:
import com.fasterxml.jackson.databind.ObjectMapper;
public class Test {
static class SomeData {
public boolean tnAvailable;
public String sTempChange;
public String getsTempChange() {
return sTempChange;
}
public void setsTempChange(String sTempChange) {
this.sTempChange = sTempChange;
}
public boolean isTnAvailable() {
return tnAvailable;
}
public void setTnAvailable(boolean tnAvailable) {
this.tnAvailable = tnAvailable;
}
}
public static void main(String[] args) {
ObjectMapper objectMapper = new ObjectMapper();
// objectMapper.configure(MapperFeature.USE_STD_BEAN_NAMING, true);
SomeData someData = new SomeData();
someData.setsTempChange("trial_000");
someData.setTnAvailable(true);
// objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.NONE);
// objectMapper.setVisibility(PropertyAccessor.GETTER, JsonAutoDetect.Visibility.ANY);
// objectMapper.setVisibility(PropertyAccessor.SETTER, JsonAutoDetect.Visibility.ANY);
// objectMapper.setVisibility(PropertyAccessor.IS_GETTER, JsonAutoDetect.Visibility.ANY);
try {
System.out.println("Serialize: " + objectMapper.writeValueAsString(someData));
String json = "{ \"tnAvailable\": false, \"sTempChange\": \"trial_001\" }";
SomeData anotherData = objectMapper.readValue(json, SomeData.class);
System.out.println("Deserialize: " + anotherData.isTnAvailable() + ", " + anotherData.getsTempChange());
} catch (Exception e) {
e.printStackTrace();
}
}
}