Java常用JSON库性能及特点比较,常用工具方法
四种库序列化,反序列化性能对比
- JMH做的序列化,反序列化的性能测试
- 10W次测试结果如下
序列化性能:FastJson>Jackson>Gson>Jsonlib
反序列化性能:Gson>Jackson>FastJson>Jsonlib
四种库对空值属性的处理
测试
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
@Setter
@Getter
@AllArgsConstructor
public class Student {
private String name;
private String password;
private Integer age;
private String[] hobbies;
private Student deskMate;
}
import com.ultra.util.FastJsonUtil;
import com.ultra.util.GsonUtil;
import com.ultra.util.JacksonUtil;
import com.ultra.util.JsonLibUtil;
import org.junit.Test;
public class JsonUtilTest {
@Test
public void testJson() {
Student student = new Student("AA", null, null, null, null);
System.out.println("FastJson: " + FastJsonUtil.getJson(student));
System.out.println("Gson: " + GsonUtil.bean2Json(student));
System.out.println("Jackson: " + JacksonUtil.bean2Json(student));
System.out.println("JsonLib: " + JsonLibUtil.bean2Json(student));
}
}
结果
FastJson: {"name":"AA"}
Gson: {"name":"AA"}
Jackson: {"name":"AA","password":null,"age":null,"hobbies":null,"deskMate":null}
JsonLib: {"age":0,"deskMate":null,"hobbies":[],"name":"AA","password":""}
结论
- FastJson:忽略空值的属性,空集合的属性
- Gson:忽略空值的属性,空集合的属性
- Jackson:空值的属性和空集合的属性值是null
- JsonLib:基本数据类型包装类会初始化值,String属性会初始化"";其他空值属性的值是null,空集合的属性值是[]
四种库的工具类
FastJsonUtil
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import java.util.List;
public class FastJsonUtil {
public static <T> T json2Bean(String jsonString, Class<T> requestType) {
return JSON.parseObject(jsonString, requestType);
}
public static <T> List<T> json2Beans(String jsonString, Class<T> requestType) {
return JSON.parseArray(jsonString, requestType);
}
public static String bean2Json(Object javaObject) {
return JSONObject.toJSONString(javaObject);
}
public static String beans2Json(Object list) {
return JSONObject.toJSONString(list);
}
}
GsonUtil
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import java.util.List;
public class GsonUtil {
private static final Gson GSON = new GsonBuilder().create();
public static String bean2Json(Object obj) {
return GSON.toJson(obj);
}
public static <T> T json2Bean(String jsonString, Class<T> objClass) {
return GSON.fromJson(jsonString, objClass);
}
public static <T> List<T> json2Beans(String jsonString) {
return GSON.fromJson(jsonString, new TypeToken<List<T>>() {
}.getType());
}
}
JacksonUtil
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class JacksonUtil {
private static final Logger logger = LoggerFactory.getLogger(JacksonUtil.class);
private static final ObjectMapper MAPPER = new ObjectMapper();
public static String bean2Json(Object obj) {
try {
return MAPPER.writeValueAsString(obj);
} catch (JsonProcessingException e) {
logger.error("", e);
return null;
}
}
public static <T> T json2Bean(String jsonString, Class<T> objClass) {
try {
return MAPPER.readValue(jsonString, objClass);
} catch (IOException e) {
logger.error("", e);
return null;
}
}
public static <T> List<T> json2Beans(String jsonString, Class<T> objClass) {
try {
JavaType javaType = getCollectionType(objClass);
return MAPPER.readValue(jsonString, javaType);
} catch (IOException e) {
logger.error("", e);
return null;
}
}
private static JavaType getCollectionType(Class<?> elementClass) {
return MAPPER.getTypeFactory().constructParametricType(ArrayList.class, elementClass);
}
}
JsonLibUtil
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class JsonLibUtil {
public static String bean2Json(Object obj) {
JSONObject jsonObject = JSONObject.fromObject(obj);
return jsonObject.toString();
}
public static String beans2Json(Object list) {
JSONArray jsonArray = JSONArray.fromObject(list);
return jsonArray.toString();
}
public static <T> T json2Bean(String jsonString, Class<T> objClass) {
return (T) JSONObject.toBean(JSONObject.fromObject(jsonString), objClass);
}
public static <T> List<T> json2Beans(String jsonString, Class<T> objClass) {
JSONArray jsonArray = JSONArray.fromObject(jsonString);
return new ArrayList<T>(JSONArray.toCollection(jsonArray, objClass));
}
}