-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
PropertyNamingStrategy doesn't work during deserialization #1832
Comments
Please re-file at Kotlin module unless reproducible with Java-only test. If latter, please update with Java-only example here and I can have a look. |
@cowtowncoder Sorry for that I thought kotlin code may contain less boilerplate code public class JacksonJavaTest {
@Test
public void testSnakeCaseDeserialization() throws IOException {
String json = "{\"my_camel_case\":10001}";
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
TestSnakeCase obj = objectMapper.readValue(json, TestSnakeCase.class);
assertEquals(Integer.valueOf(10001), obj.getMyCamelCase());
}
@Test
public void testSnakeCaseSerialization() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
TestSnakeCase obj = new TestSnakeCase(10001);
System.out.println(objectMapper.writeValueAsString(obj));
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
static class TestSnakeCase {
private Integer myCamelCase;
@JsonCreator
public TestSnakeCase(Integer myCamelCase) {
this.myCamelCase = myCamelCase;
}
public Integer getMyCamelCase() {
return myCamelCase;
}
public void setMyCamelCase(Integer myCamelCase) {
this.myCamelCase = myCamelCase;
}
}
} |
It looks like the public class JacksonJavaTest {
@Test
public void testSnakeCaseDeserialization() throws IOException {
String json = "{\"my_camel_case\":10001}";
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
TestSnakeCase obj = objectMapper.readValue(json, TestSnakeCase.class);
assertEquals(Integer.valueOf(10001), obj.getMyCamelCase());
}
@Test
public void testSnakeCaseSerialization() throws JsonProcessingException {
ObjectMapper objectMapper = new ObjectMapper().findAndRegisterModules();
TestSnakeCase obj = new TestSnakeCase(10001);
System.out.println(objectMapper.writeValueAsString(obj));
}
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
static class TestSnakeCase {
private Integer myCamelCase;
public TestSnakeCase() {
}
public TestSnakeCase(Integer myCamelCase) {
this.myCamelCase = myCamelCase;
}
public Integer getMyCamelCase() {
return myCamelCase;
}
public void setMyCamelCase(Integer myCamelCase) {
this.myCamelCase = myCamelCase;
}
}
} |
Actually I think problem here could be bit different. But first the usual question: which version is this with? Is it reproducible on 2.9.2? If yes, does using
make difference? It should as by default I think mode would be auto-detected as |
Oops, This is interesting, I didn't notice it only happens on POJO that has exactly one property. |
@unlimitedsola Yes this is.... sort of unfortunate thing, and commonly stumbled upon. So there are two interpretations here: with just 1 property, one could either consider mapping exact value ("delegating"), or JSON Object of just one property ("property-based"). Jackson tries to use heuristics if not explicitly specified but I don't think there is anything that can ultimately tell it, short of allowing configuration for defaulting (mode used if not annotated) and allowing overrides.
|
simple kotlin snippet to reproduce:
also happens in Java
The text was updated successfully, but these errors were encountered: