I would like to deserialize json
strings that include camel case or snake case as follow.
{"testField":"test"}
or
{"test_field":"test"}
And, I would like to deserialize this json strings by only one ObjectMapper objeect as follow.
public class Test {
public static void main(final String[] args) throws JsonProcessingException {
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
System.out.println(objectMapper.readValue("{\"testField\":\"test\"}", Parent.class));
System.out
.println(objectMapper.readValue("{\"test_field\":\"test\"}", Parent.class));
}
@Data
@AllArgsConstructor
@NoArgsConstructor
static class Parent {
String testField;
}
}
But, I could deserialize into either a camel case or a snake case.
Additionally, I want only snake case during serialization.
I tried a custom PropertyNamingStrategy.
But it did not work well.
You can use @JsonAlias
class Parent {
@JsonAlias({ "test_field" })
String testField;
}