Skip to content

Commit 62b2fea

Browse files
vunamtientienvn4
andauthored
BAEL-5193-Deserialize Snake Case With Jackson (eugenp#11341)
* BAEL-5193-deserialize-snake-case * refactor * refactor Co-authored-by: tienvn4 <[email protected]>
1 parent ca8bead commit 62b2fea

4 files changed

Lines changed: 118 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.jackson.snakecase;
2+
3+
public class User {
4+
private String firstName;
5+
private String lastName;
6+
7+
public String getFirstName() {
8+
return firstName;
9+
}
10+
11+
public void setFirstName(String firstName) {
12+
this.firstName = firstName;
13+
}
14+
15+
public String getLastName() {
16+
return lastName;
17+
}
18+
19+
public void setLastName(String lastName) {
20+
this.lastName = lastName;
21+
}
22+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jackson.snakecase;
2+
3+
import com.fasterxml.jackson.annotation.JsonProperty;
4+
5+
public class UserWithPropertyNames {
6+
@JsonProperty("first_name")
7+
private String firstName;
8+
@JsonProperty("last_name")
9+
private String lastName;
10+
11+
public String getFirstName() {
12+
return firstName;
13+
}
14+
15+
public void setFirstName(String firstName) {
16+
this.firstName = firstName;
17+
}
18+
19+
public String getLastName() {
20+
return lastName;
21+
}
22+
23+
public void setLastName(String lastName) {
24+
this.lastName = lastName;
25+
}
26+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.baeldung.jackson.snakecase;
2+
3+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
4+
import com.fasterxml.jackson.databind.annotation.JsonNaming;
5+
6+
@JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class)
7+
public class UserWithSnakeStrategy {
8+
private String firstName;
9+
private String lastName;
10+
11+
public String getFirstName() {
12+
return firstName;
13+
}
14+
15+
public void setFirstName(String firstName) {
16+
this.firstName = firstName;
17+
}
18+
19+
public String getLastName() {
20+
return lastName;
21+
}
22+
23+
public void setLastName(String lastName) {
24+
this.lastName = lastName;
25+
}
26+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.jackson.snakecase;
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper;
4+
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
5+
import com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class SnakeCaseUnitTest {
11+
12+
private static final String JSON = "{\"first_name\": \"Jackie\", \"last_name\": \"Chan\"}";
13+
14+
@Test(expected = UnrecognizedPropertyException.class)
15+
public void whenExceptionThrown_thenExpectationSatisfied() throws Exception {
16+
ObjectMapper objectMapper = new ObjectMapper();
17+
objectMapper.readValue(JSON, User.class);
18+
}
19+
20+
@Test
21+
public void givenSnakeCaseJson_whenParseWithJsonPropertyAnnotation_thenGetExpectedObject() throws Exception {
22+
ObjectMapper objectMapper = new ObjectMapper();
23+
UserWithPropertyNames user = objectMapper.readValue(JSON, UserWithPropertyNames.class);
24+
assertEquals("Jackie", user.getFirstName());
25+
assertEquals("Chan", user.getLastName());
26+
}
27+
28+
@Test
29+
public void givenSnakeCaseJson_whenParseWithJsonNamingAnnotation_thenGetExpectedObject() throws Exception {
30+
ObjectMapper objectMapper = new ObjectMapper();
31+
UserWithSnakeStrategy user = objectMapper.readValue(JSON, UserWithSnakeStrategy.class);
32+
assertEquals("Jackie", user.getFirstName());
33+
assertEquals("Chan", user.getLastName());
34+
}
35+
36+
@Test
37+
public void givenSnakeCaseJson_whenParseWithCustomMapper_thenGetExpectedObject() throws Exception {
38+
ObjectMapper objectMapper = new ObjectMapper().setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE);
39+
User user = objectMapper.readValue(JSON, User.class);
40+
assertEquals("Jackie", user.getFirstName());
41+
assertEquals("Chan", user.getLastName());
42+
}
43+
44+
}

0 commit comments

Comments
 (0)