Gson and Jackson both are the very powerful and popular libraries to convert Java Object to JSON and vice versa. This Spring boot Gson and Jackson tutorial aims to explain, serialization and deserialization of Java objects using Gson and Jackson.
Spring boot by default uses Jackson for Java Object conversion and for using Jackson, we do not have to add any additional dependency.
Jackson library is part of
spring-boot-starter-json which are already available in
spring-boot-starter-web.So when we include
spring-boot-starter-web in the project, then we are not supposed to add any additional dependency.
While Gson is not by default provided by spring boot, So for using Gson for serialization and deserialization, we have to add an additional dependency in build tools like Maven or Gradle.
2. Spring Jackson tutorial
- For using Jackson, We just have to add
spring-boot-starter-webin Maven, Gradle build file or in classpath.
- We have already explained how to convert response to JSON using Jackson in Spring boot project.
- Jackson will use reflation for converting Objects to JSON and vice versa, It will by default use camel notation as key’s name in JSON, but Jackson provides easy configuration for changing naming strategy.
- Our article on Spring Jackson property naming strategy gives broader idea regarding changing naming strategy property in Spring Jackson.
- While conversion, Jackson will convert Object’s fields in JSON, We can exclude null fields while converting JSON, In one of our previous articles, we have explained how Spring Jackson excludes null fields.
- Jackson also provides easy configuration to convert Date in different Java format or Timezone. Here is an article which explains Spring Jackson custom date format.
- List of articles for Spring Jackson:
3. Spring Gson tutorial
- If you are new with Spring and If you want to configure Gson then here is an article about how to configure Gson in spring boot.
- Gson uses reflection to convert Java objects to JSON and vice-versa, and Java uses camel notation for the variable declaration and same variable name converted in JSON.
- Many times we want to manage different standards for JSON conversion. Gson also provides easy options to change naming strategy and you may have a look at our in depth article for changing Gson field naming strategy in Spring.
- The other characteristics of Gson is that, at the time to serializing it will ignore null fields, but also provide easy configurations to include null fields in JSON, Here is detail article for serializing null in Gson
- Gson also provides the ways of JSON customization for serialization and de-serialization, Here is an example for customizing date format with Gson.
- List of articles for Spring Gson:
In this tutorial page of Jackson and Gson, We have learnt about configuration and customization of Jackson and Gson as well. We have provided links for our articles with more description and source code.