1. Overview

Google’s Gson is very popular library for converting Java Object to Json and in this article we will learn different strategy for converting java field name to upper case, lower case, camel case, We have explain here configuration of Gson field naming strategy in spring boot or we can say how to change Gson field naming strategy in Spring or spring boot application.

If you are not aware of how to configure Gson in spring boot application then here is details for same.

Gson and Jackson both are different library from different wander to convert java Object to Json format. If you are looking for naming strategy for Jackson then here is article for same.

Sometimes developer may get confused that which properties is better to use, then i like to suggest that based on our requirements and standards of application development we can use any of those naming strategy but one my favorite is camel notation strategy.

2. Ways to configure spring Gson field naming strategy

In spring boot application we have configuration spring.gson.field-naming-policy that we can configure in application.properties or application.yml. Here details for application.properties and it’s default location in spring boot application. For spring mvc(without spring boot) we can use GsonBuilder bean for customization of Gson configurations.

2.1 application.properties

Here is possible for naming strategy which is supported by Gson:

  1. upper_camel_case (i.e someFieldName —> SomeFieldName)
  2. lower_case_with_dashes (i.e someFieldName —> some.field.name)
  3. lower_case_with_underscores (i.e someFieldName —> some_field_name)
  4. identity (same as field name, no change)
  5. lower_case_with_dots (i.e someFieldName —> some.field.name)
  6. upper_camel_case_with_spaces (i.e someFieldName —> Some Field Name)

NOTE: identity is default naming strategy

application.properties

spring.gson.field-naming-policy=upper_camel_case

2.2 Using GsonBuilder Bean

Here we have created GsonBuilder bean which contains configuration related to naming strategy. Here are list of constants for possible values:

  • FieldNamingPolicy.IDENTITY
  • FieldNamingPolicy.UPPER_CAMEL_CASE
  • FieldNamingPolicy.UPPER_CAMEL_CASE_WITH_SPACES
  • FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES
  • FieldNamingPolicy.LOWER_CASE_WITH_DASHES
  • FieldNamingPolicy.LOWER_CASE_WITH_DOTS
@Bean
GsonBuilder gsonBuilder(){
    GsonBuilder gsonBuilder=new GsonBuilder();
    gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE);
    return gsonBuilder;
}

2.3 Custom naming strategy

Requirements has not limit !. So many times we need to write our own custom logic for fields name in that case Gson also provide easy way configure custom logic for properties name. GsonBuilder has setFieldNamingStrategymethod to set custom naming strategy. FieldNamingStrategy is interface which has translateNamemethod where can write our custom logic. Here is example for the same:

@Bean
GsonBuilder gsonBuilder(){
    GsonBuilder gsonBuilder=new GsonBuilder();
    gsonBuilder.setFieldNamingStrategy(new FieldNamingStrategy() {
        @Override
        public String translateName(Field field) {
            return field.getName().toUpperCase();   // write custom logic here
        }
    });
    return gsonBuilder;
}

3. Conclusion

In this article, We learn that how we can change default naming strategy to another naming strategy and also learn about how to create our own naming strategy for properties name.

4. References

5. Source Code

Git : spring-boot-gson-field-naming-strategy

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *