Jackson - Case insensitive deserialization
Jackson is a well-known library for JSON utilities. It has a wide area of features. One of them is case insensitive deserialization for field names. It’s available since 2.5.0. In this post, I’ll give an example of such case.
Here is a sample POJO class:
public class CarInfo {
private String model;
private String year;
//getter, setter, constructor
}
And here are two JSON messages we’d like to deserialize. First:
{
"model":"Tesla",
"year":"2015"
}
This JSON string is a valid one and it can be easily deserialized by following code snippet:
ObjectMapper objectMapper = new ObjectMapper();
CarInfo info = objectMapper.readValue(data, CarInfo.class); //'data' contains JSON string
Below is the second JSON message:
{
"ModeL":"Tesla",
"YeaR":"2015"
}
Normally, default ObjectMapper
cannot deserialize this message into a
CarInfo
object. With following configuration, it’s possible:
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);
CarInfo info = objectMapper.readValue(data, CarInfo.class); //'data' contains JSON string
This deserialization is valid. his deserialization is valid.