Jackson overcomes underscores in favor of a camel event

I am retrieving a JSON string from the Internet; like most JSON, I saw that it includes long keys separated by underscores. Essentially, my goal is to deserialize JSON into java objects, but I don't use underscores in Java code.

For example, I can have a User class with the firstName field in the camel case, at the same time I need to somehow tell Jackson to map the first_name key from the JSON class field to firstName . Is it possible?

 class User{ protected String firstName; protected String getFirstName(){return firstName;} } 
+131
java jackson
May 09 '12 at 15:30
source share
7 answers

You must use @JsonProperty in the field that you want to change the default name mapping.

 class User{ @JsonProperty("first_name") protected String firstName; protected String getFirstName(){return firstName;} } 

For more information: API

+86
May 9 '12 at 15:33
source share

You can configure ObjectMapper to convert camel case to underscore names:

 this.objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); 

Before Jackson 2.7, the constant was called:

 PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES 
+278
Jul 24 '12 at 13:35
source share

If it's a spring boot application, in application.properties file just use

spring.jackson.property naming-strategy = SNAKE_CASE

Or Annotate a model class using this annotation.

@JsonNaming (PropertyNamingStrategy.SnakeCaseStrategy.class)

+91
Nov 21 '16 at 10:12
source share

If you want this for one class, you can use PropertyNamingStrategy with @JsonNaming , something like this:

 @JsonNaming(PropertyNamingStrategy.LowerCaseWithUnderscoresStrategy.class) public static class Request { String businessName; String businessLegalName; } 

Will serialize to:

 { "business_name" : "", "business_legal_name" : "" } 

Starting with Jackson 2.7 LowerCaseWithUnderscoresStrategy deprecated in favor of SnakeCaseStrategy , so you should use:

 @JsonNaming(PropertyNamingStrategy.SnakeCaseStrategy.class) public static class Request { String businessName; String businessLegalName; } 
+34
Sep 12 '16 at 23:20
source share

The above answers regarding @JsonProperty and CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES 100% accurate, although some people (like me) may try to do this in a Spring MVC code-based application. Here is a sample code (which I have inside Beans.java ) to achieve the desired effect:

 @Bean public ObjectMapper jacksonObjectMapper() { return new ObjectMapper().setPropertyNamingStrategy( PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); } 
+25
Jan 13 '15 at 15:10
source share

It is currently recommended that you configure Jackson in the application.yml (or properties ) file.

Example:

 spring: jackson: property-naming-strategy: SNAKE_CASE 

If you have more complex configuration requirements, you can also configure Jackson programmatically.

 import com.fasterxml.jackson.databind.PropertyNamingStrategy; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder; @Configuration public class JacksonConfiguration { @Bean public Jackson2ObjectMapperBuilder jackson2ObjectMapperBuilder() { return new Jackson2ObjectMapperBuilder() .propertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE); // insert other configurations } } 
+8
Apr 7 '17 at 6:28
source share

Below are a few answers indicating both strategies for two different versions of the Jackson library:

For Jackson 2.6. *

 ObjectMapper objMapper = new ObjectMapper(new JsonFactory()); // or YAMLFactory() objMapper.setNamingStrategy( PropertyNamingStrategy.CAMEL_CASE_TO_LOWER_CASE_WITH_UNDERSCORES); 

For Jackson 2.7. *

 ObjectMapper objMapper = new ObjectMapper(new JsonFactory()); // or YAMLFactory() objMapper.setNamingStrategy( PropertyNamingStrategy.SNAKE_CASE); 
+6
Apr 05 '18 at 19:11
source share



All Articles