Skip to content

Commit

Permalink
Add support for @JsonNaming to jackson-databind (#483)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Vitale <[email protected]>
  • Loading branch information
ThomasVitale authored Jul 24, 2024
1 parent 7bc1546 commit e9f8af0
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,4 +1,60 @@
[
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$UpperSnakeCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$LowerCamelCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$UpperCamelCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$LowerCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$KebabCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.PropertyNamingStrategies$LowerDotCaseStrategy",
"allDeclaredConstructors": true,
"methods":[{"name":"<init>","parameterTypes":[] }],
"condition": {
"typeReachable": "com.fasterxml.jackson.databind.PropertyNamingStrategies"
}
},
{
"name": "com.fasterxml.jackson.databind.deser.std.DateDeserializers$SqlDateDeserializer",
"allDeclaredConstructors": true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
"metadata-version": "2.15.2",
"module": "com.fasterxml.jackson.core:jackson-databind",
"tested-versions": [
"2.15.2"
"2.15.2",
"2.15.4"
]
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Copyright and related rights waived via CC0
*
* You should have received a copy of the CC0 legalcode along with this
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
*/
package com_fasterxml_jackson_core.jackson_databind;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.annotation.JsonNaming;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

class JacksonNamingStrategyTest {

static final ObjectMapper mapper = new ObjectMapper();

// SnakeCaseStrategy

@JsonNaming(PropertyNamingStrategies.SnakeCaseStrategy.class)
record BeanSnake(String coffeeType) {}

@Test
void serializeObjectWithSnakeCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanSnake("espresso"));
assertThat(result).isEqualTo("{\"coffee_type\":\"espresso\"}");
}

@Test
void deserializeObjectWithSnakeCaseStrategy() throws JsonProcessingException {
BeanSnake bean = mapper.readValue("{\"coffee_type\":\"espresso\"}", BeanSnake.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// UpperSnakeCaseStrategy

@JsonNaming(PropertyNamingStrategies.UpperSnakeCaseStrategy.class)
record BeanUpperSnake(String coffeeType) {}

@Test
void serializeObjectWithUpperSnakeCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanUpperSnake("espresso"));
assertThat(result).isEqualTo("{\"COFFEE_TYPE\":\"espresso\"}");
}

@Test
void deserializeObjectWithUpperSnakeCaseStrategy() throws JsonProcessingException {
BeanUpperSnake bean = mapper.readValue("{\"COFFEE_TYPE\":\"espresso\"}", BeanUpperSnake.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// LowerCamelCaseStrategy

@JsonNaming(PropertyNamingStrategies.LowerCamelCaseStrategy.class)
record BeanLowerCamel(String coffeeType) {}

@Test
void serializeObjectWithLowerCamelCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanLowerCamel("espresso"));
assertThat(result).isEqualTo("{\"coffeeType\":\"espresso\"}");
}

@Test
void deserializeObjectWithLowerCamelCaseStrategy() throws JsonProcessingException {
BeanLowerCamel bean = mapper.readValue("{\"coffeeType\":\"espresso\"}", BeanLowerCamel.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// UpperCamelCaseStrategy

@JsonNaming(PropertyNamingStrategies.UpperCamelCaseStrategy.class)
record BeanUpperCamel(String coffeeType) {}

@Test
void serializeObjectWithUpperCamelCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanUpperCamel("espresso"));
assertThat(result).isEqualTo("{\"CoffeeType\":\"espresso\"}");
}

@Test
void deserializeObjectWithUpperCamelCaseStrategy() throws JsonProcessingException {
BeanUpperCamel bean = mapper.readValue("{\"CoffeeType\":\"espresso\"}", BeanUpperCamel.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// LowerCaseStrategy

@JsonNaming(PropertyNamingStrategies.LowerCaseStrategy.class)
record BeanLower(String coffeeType) {}

@Test
void serializeObjectWithLowerCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanLower("espresso"));
assertThat(result).isEqualTo("{\"coffeetype\":\"espresso\"}");
}

@Test
void deserializeObjectWithLowerCaseStrategy() throws JsonProcessingException {
BeanLower bean = mapper.readValue("{\"coffeetype\":\"espresso\"}", BeanLower.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// KebabCaseStrategy

@JsonNaming(PropertyNamingStrategies.KebabCaseStrategy.class)
record BeanKebab(String coffeeType) {}

@Test
void serializeObjectWithKebabCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanKebab("espresso"));
assertThat(result).isEqualTo("{\"coffee-type\":\"espresso\"}");
}

@Test
void deserializeObjectWithKebabCaseStrategy() throws JsonProcessingException {
BeanKebab bean = mapper.readValue("{\"coffee-type\":\"espresso\"}", BeanKebab.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

// LowerDotCaseStrategy

@JsonNaming(PropertyNamingStrategies.LowerDotCaseStrategy.class)
record BeanLowerDot(String coffeeType) {}

@Test
void serializeObjectWithLowerDotCaseStrategy() throws JsonProcessingException {
String result = mapper.writeValueAsString(new BeanLowerDot("espresso"));
assertThat(result).isEqualTo("{\"coffee.type\":\"espresso\"}");
}

@Test
void deserializeObjectWithLowerDotCaseStrategy() throws JsonProcessingException {
BeanLowerDot bean = mapper.readValue("{\"coffee.type\":\"espresso\"}", BeanLowerDot.class);
assertThat(bean.coffeeType()).isEqualTo("espresso");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,47 @@
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanSnake",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanUpperSnake",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanLowerCamel",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanUpperCamel",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanLower",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanKebab",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
},
{
"name": "com_fasterxml_jackson_core.jackson_databind.JacksonNamingStrategyTest$BeanLowerDot",
"allDeclaredConstructors": true,
"allDeclaredMethods": true,
"allDeclaredFields": true
}
]

0 comments on commit e9f8af0

Please sign in to comment.