Performance Benchmark of Common Java JSON Libraries (Gson, FastJson, Jackson, Json-lib)
This article presents a JMH‑based performance comparison of four popular Java JSON libraries—Gson, FastJson, Jackson, and Json‑lib—covering their Maven dependencies, utility wrappers, a realistic Person model, serialization and deserialization benchmarks, and analysis of the results to guide library selection.
This article uses JMH to benchmark the performance of four widely used Java JSON libraries—Gson, FastJson, Jackson, and Json‑lib—by measuring both serialization and deserialization speeds on a realistic data model.
Simple Introduction
When choosing a JSON library, factors such as string‑to‑JSON parsing speed, bean‑to‑JSON conversion speed, collection handling, and ease of use should be considered.
Gson
Project address: https://github.com/google/gson
Gson is a feature‑complete JSON parser developed by Google, requiring no extra dependencies and supporting conversion via toJson and fromJson .
FastJson
Project address: https://github.com/alibaba/fastjson
FastJson, created by Alibaba, is a high‑performance JSON processor with no external dependencies, though it may need special handling for complex bean references.
Jackson
Project address: https://github.com/FasterXML/jackson
Jackson is the most widely adopted JSON library in the Java ecosystem, offering a small set of core jars, fast parsing of large files, low memory footprint, and a flexible API.
Json‑lib
Project address: http://json-lib.sourceforge.net/index.html
Json‑lib was once popular but now suffers from many third‑party dependencies and poor performance on complex types.
Adding Maven Dependencies
<!-- Json libs-->
<dependency>
<groupId>net.sf.json-lib</groupId>
<artifactId>json-lib</artifactId>
<version>2.4</version>
<classifier>jdk15</classifier>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.4</version>
</dependency>Utility Classes for Each Library
public class FastJsonUtil {
public static String bean2Json(Object obj) {
return JSON.toJSONString(obj);
}
public static
T json2Bean(String jsonStr, Class
objClass) {
return JSON.parseObject(jsonStr, objClass);
}
} public class GsonUtil {
private static Gson gson = new GsonBuilder().create();
public static String bean2Json(Object obj) { return gson.toJson(obj); }
public static
T json2Bean(String jsonStr, Class
objClass) { return gson.fromJson(jsonStr, objClass); }
public static String jsonFormatter(String uglyJsonStr) {
Gson pretty = new GsonBuilder().setPrettyPrinting().create();
JsonParser jp = new JsonParser();
JsonElement je = jp.parse(uglyJsonStr);
return pretty.toJson(je);
}
} public class JacksonUtil {
private static ObjectMapper mapper = new ObjectMapper();
public static String bean2Json(Object obj) {
try { return mapper.writeValueAsString(obj); } catch (JsonProcessingException e) { e.printStackTrace(); return null; }
}
public static
T json2Bean(String jsonStr, Class
objClass) {
try { return mapper.readValue(jsonStr, objClass); } catch (IOException e) { e.printStackTrace(); return null; }
}
} public class JsonLibUtil {
public static String bean2Json(Object obj) {
JSONObject jsonObject = JSONObject.fromObject(obj);
return jsonObject.toString();
}
@SuppressWarnings("unchecked")
public static
T json2Bean(String jsonStr, Class
objClass) {
return (T) JSONObject.toBean(JSONObject.fromObject(jsonStr), objClass);
}
}Model Classes
public class Person {
private String name;
private FullName fullName;
private int age;
private Date birthday;
private List
hobbies;
private Map
clothes;
private List
friends;
// getters/setters omitted
@Override public String toString() { /* builds a detailed string */ }
}
public class FullName {
private String firstName;
private String middleName;
private String lastName;
// constructors, getters/setters, toString omitted
}JSON Serialization Benchmark (JMH)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class JsonSerializeBenchmark {
@Param({"1000","10000","100000"}) private int count;
private Person p;
@Setup public void prepare() { /* create Person with friends, hobbies, etc. */ }
@Benchmark public void JsonLib() { for (int i=0;iJSON Deserialization Benchmark (JMH)
@BenchmarkMode(Mode.SingleShotTime)
@OutputTimeUnit(TimeUnit.SECONDS)
@State(Scope.Benchmark)
public class JsonDeserializeBenchmark {
@Param({"1000","10000","100000"}) private int count;
private String jsonStr;
@Setup public void prepare() { jsonStr = "{...}"; }
@Benchmark public void JsonLib() { for (int i=0;iThe benchmark results show that for a small number of operations Gson is the fastest, but as the count grows to 100 000, FastJson and Jackson outperform Gson, while Json‑lib remains significantly slower. In deserialization, Gson, FastJson and Jackson have comparable performance, all far ahead of Json‑lib.
Conclusion: Choose Gson for low‑volume scenarios, but prefer Jackson or FastJson for high‑throughput applications; avoid Json‑lib for performance‑critical code.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.