Top 5 Libraries for Serialization and Deserialization JSON in Java
Tags: Java JSON gson jackson-databind Jackson fastjson moshi jsoniter
In this blog post we will go through 5 useful open source libraries to serialization and deserialize JSON data in Java application. For each library we will write a simple code sample to show you how to use the library to convert Java object to a JSON string and vice-versa.
Gson
Github repository: github.com/google/gson
Adding Gson dependency into your project
Using Gradle
compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
Using Maven
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
How to serialize object to JSON string using Gson
List<Employee> employees = ..;
Gson gson = new Gson();
String jsonStringFromObject = gson.toJson(employees);
How to deserialize JSON string to Java object using Gson
String jsonString = ...;
Gson gson = new Gson();
List<Employee> objectFromJsonString = gson.fromJson(jsonString, List.class);
Jackson
Github repository: github.com/FasterXML/jackson
Adding Jackson dependency into your project
Using Gradle
compile group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.9.8'
Using Maven
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
How to serialize object to JSON string using Jackson
List<Employee> employees = ...;
ObjectMapper objectMapper = new ObjectMapper();
String jsonStringFromObject = objectMapper.writeValueAsString(employees);
How to deserialize JSON string to Java object using Jackson
String jsonString = ...;
ObjectMapper objectMapper = new ObjectMapper();
List<Employee> objectFromJsonString = objectMapper.readValue(jsonString, List.class);
Fastjson
Github repository: github.com/alibaba/fastjson
Adding Fastjson dependency into your project
Using Gradle
compile group: 'com.alibaba', name: 'fastjson', version: '1.2.56'
Using Maven
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.56</version>
</dependency>
How to serialize object to JSON string using Fastjson
List<Employee> employees = ...;
String jsonStringFromObject = JSON.toJSONString(employees);
How to deserialize JSON string to Java object using Fastjson
String jsonString = ...;
List<Employee> objectFromJsonString = JSON.parseObject(jsonString, List.class);
Moshi
Github repository: github.com/square/moshi
Adding Moshi dependency into your project
Using Gradle
compile group: 'com.squareup.moshi', name: 'moshi', version: '1.8.0'
Using Maven
<dependency>
<groupId>com.squareup.moshi</groupId>
<artifactId>moshi</artifactId>
<version>1.8.0</version>
</dependency>
How to serialize object to JSON string using Moshi
List<Employee> employees = ...;
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<List> jsonAdapter = moshi.adapter(List.class);
String jsonStringFromObject = jsonAdapter.toJson(employees);
How to deserialize JSON string to Java object using Moshi
String jsonString = ...;
Moshi moshi = new Moshi.Builder().build();
JsonAdapter<List> jsonAdapter = moshi.adapter(List.class);
List<Employee> objectFromJsonString = jsonAdapter.fromJson(jsonString);
Jsoniter
Github repository: github.com/json-iterator/java
Adding Jsoniter dependency into your project
Using Gradle
compile group: 'com.jsoniter', name: 'jsoniter', version: '0.9.23'
Using Maven
<dependency>
<groupId>com.jsoniter</groupId>
<artifactId>jsoniter</artifactId>
<version>0.9.23</version>
</dependency>
How to serialize object to JSON string using Jsoniter
List<Employee> employees = ...;
String jsonStringFromObject = JsonStream.serialize(employees);
How to deserialize JSON string to Java object using Jsoniter
String jsonString = ...);
List<Employee> objectFromJsonString = JsonIterator.deserialize(jsonString, List.class);
Download Source Code
The full source code in this blog can be found at: https://github.com/simplesolutiondev/SerializationDeserializationJSON
Happy Coding!