|
7 | 7 |
|
8 | 8 | import com.fasterxml.jackson.annotation.*; |
9 | 9 | import com.fasterxml.jackson.databind.BaseMapTest; |
| 10 | +import com.fasterxml.jackson.databind.MapperFeature; |
10 | 11 | import com.fasterxml.jackson.databind.ObjectMapper; |
11 | 12 | import com.fasterxml.jackson.databind.PropertyNamingStrategy; |
12 | 13 | import com.fasterxml.jackson.databind.annotation.JsonNaming; |
@@ -316,4 +317,41 @@ public void testNamingWithObjectNode() throws Exception |
316 | 317 | assertEquals(2, result.json.size()); |
317 | 318 | assertEquals("bing", result.json.path("baz").asText()); |
318 | 319 | } |
| 320 | + |
| 321 | + static class ExplicitBean { |
| 322 | + @JsonProperty("firstName") |
| 323 | + String userFirstName = "Peter"; |
| 324 | + @JsonProperty("lastName") |
| 325 | + String userLastName = "Venkman"; |
| 326 | + @JsonProperty |
| 327 | + String userAge = "35"; |
| 328 | + } |
| 329 | + |
| 330 | + public void testExplicitRename() throws Exception { |
| 331 | + ObjectMapper m = new ObjectMapper(); |
| 332 | + m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); |
| 333 | + m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); |
| 334 | + // by default, renaming will not take place on explicitly named fields |
| 335 | + assertEquals(aposToQuotes("{'firstName':'Peter','lastName':'Venkman','user_age':'35'}"), |
| 336 | + m.writeValueAsString(new ExplicitBean())); |
| 337 | + |
| 338 | + m = new ObjectMapper(); |
| 339 | + m.setPropertyNamingStrategy(PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); |
| 340 | + m.enable(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY); |
| 341 | + m.enable(MapperFeature.ALLOW_EXPLICIT_PROPERTY_RENAMING); |
| 342 | + // w/ feature enabled, ALL property names should get re-written |
| 343 | + assertEquals(aposToQuotes("{'first_name':'Peter','last_name':'Venkman','user_age':'35'}"), |
| 344 | + m.writeValueAsString(new ExplicitBean())); |
| 345 | + |
| 346 | + // test deserialization as well |
| 347 | + ExplicitBean bean = |
| 348 | + m.readValue(aposToQuotes("{'first_name':'Egon','last_name':'Spengler','user_age':'32'}"), |
| 349 | + ExplicitBean.class); |
| 350 | + |
| 351 | + assertNotNull(bean); |
| 352 | + assertEquals("Egon", bean.userFirstName); |
| 353 | + assertEquals("Spengler", bean.userLastName); |
| 354 | + assertEquals("32", bean.userAge); |
| 355 | + |
| 356 | + } |
319 | 357 | } |
0 commit comments