From 1adb96e3846ae28d78a80c9be7da2f0e139f08cc Mon Sep 17 00:00:00 2001 From: vunamtien Date: Wed, 3 Nov 2021 16:22:59 +0700 Subject: [PATCH] BAEL-5193-Deserialize Snake Case With Jackson (#11341) * BAEL-5193-deserialize-snake-case * refactor * refactor Co-authored-by: tienvn4 --- .../com/baeldung/jackson/snakecase/User.java | 22 ++++++++++ .../snakecase/UserWithPropertyNames.java | 26 +++++++++++ .../snakecase/UserWithSnakeStrategy.java | 26 +++++++++++ .../jackson/snakecase/SnakeCaseUnitTest.java | 44 +++++++++++++++++++ 4 files changed, 118 insertions(+) create mode 100644 jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/User.java create mode 100644 jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithPropertyNames.java create mode 100644 jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithSnakeStrategy.java create mode 100644 jackson-modules/jackson-conversions-2/src/test/java/com/baeldung/jackson/snakecase/SnakeCaseUnitTest.java diff --git a/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/User.java b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/User.java new file mode 100644 index 0000000000..17bf6f898b --- /dev/null +++ b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/User.java @@ -0,0 +1,22 @@ +package com.baeldung.jackson.snakecase; + +public class User { + private String firstName; + private String lastName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithPropertyNames.java b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithPropertyNames.java new file mode 100644 index 0000000000..f9f36243ad --- /dev/null +++ b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithPropertyNames.java @@ -0,0 +1,26 @@ +package com.baeldung.jackson.snakecase; + +import com.fasterxml.jackson.annotation.JsonProperty; + +public class UserWithPropertyNames { + @JsonProperty("first_name") + private String firstName; + @JsonProperty("last_name") + private String lastName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithSnakeStrategy.java b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithSnakeStrategy.java new file mode 100644 index 0000000000..ffec940ed8 --- /dev/null +++ b/jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithSnakeStrategy.java @@ -0,0 +1,26 @@ +package com.baeldung.jackson.snakecase; + +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.annotation.JsonNaming; + +@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) +public class UserWithSnakeStrategy { + private String firstName; + private String lastName; + + public String getFirstName() { + return firstName; + } + + public void setFirstName(String firstName) { + this.firstName = firstName; + } + + public String getLastName() { + return lastName; + } + + public void setLastName(String lastName) { + this.lastName = lastName; + } +} diff --git a/jackson-modules/jackson-conversions-2/src/test/java/com/baeldung/jackson/snakecase/SnakeCaseUnitTest.java b/jackson-modules/jackson-conversions-2/src/test/java/com/baeldung/jackson/snakecase/SnakeCaseUnitTest.java new file mode 100644 index 0000000000..c72908ccce --- /dev/null +++ b/jackson-modules/jackson-conversions-2/src/test/java/com/baeldung/jackson/snakecase/SnakeCaseUnitTest.java @@ -0,0 +1,44 @@ +package com.baeldung.jackson.snakecase; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategy; +import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class SnakeCaseUnitTest { + + private static final String JSON = "{\"first_name\": \"Jackie\", \"last_name\": \"Chan\"}"; + + @Test(expected = UnrecognizedPropertyException.class) + public void whenExceptionThrown_thenExpectationSatisfied() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + objectMapper.readValue(JSON, User.class); + } + + @Test + public void givenSnakeCaseJson_whenParseWithJsonPropertyAnnotation_thenGetExpectedObject() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + UserWithPropertyNames user = objectMapper.readValue(JSON, UserWithPropertyNames.class); + assertEquals("Jackie", user.getFirstName()); + assertEquals("Chan", user.getLastName()); + } + + @Test + public void givenSnakeCaseJson_whenParseWithJsonNamingAnnotation_thenGetExpectedObject() throws Exception { + ObjectMapper objectMapper = new ObjectMapper(); + UserWithSnakeStrategy user = objectMapper.readValue(JSON, UserWithSnakeStrategy.class); + assertEquals("Jackie", user.getFirstName()); + assertEquals("Chan", user.getLastName()); + } + + @Test + public void givenSnakeCaseJson_whenParseWithCustomMapper_thenGetExpectedObject() throws Exception { + ObjectMapper objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); + User user = objectMapper.readValue(JSON, User.class); + assertEquals("Jackie", user.getFirstName()); + assertEquals("Chan", user.getLastName()); + } + +}