forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BAEL-5193-Deserialize Snake Case With Jackson (eugenp#11341)
* BAEL-5193-deserialize-snake-case * refactor * refactor Co-authored-by: tienvn4 <tienvn4@ghtk.co>
- Loading branch information
Showing
4 changed files
with
118 additions
and
0 deletions.
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
jackson-modules/jackson-conversions-2/src/main/java/com/baeldung/jackson/snakecase/User.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...son-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithPropertyNames.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...son-conversions-2/src/main/java/com/baeldung/jackson/snakecase/UserWithSnakeStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
...jackson-conversions-2/src/test/java/com/baeldung/jackson/snakecase/SnakeCaseUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
|
||
} |