推荐 jackson

 

【性能测试】JSON工具 对比 fastjson jackson_json

 

 

依赖

<!-- fastjson https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>


<!-- 工具集 https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.8</version>
</dependency>


<!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.1</version>
</dependency>

 

 

 

 

 

 

 

JsonUtil (jackson)
package com.gw.common.utils;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
* @Title: jackson工具类
* @Description: 描述
* @Version: v1.0
* @Author: Mr.Guan
* @Mail GuanWeiMail@163.com
* @DateTime: 2021-02-24
* @Project springcloud-gw-parent
* @Package com.guanwei.study.json
*/
public class JsonUtil {

/**
* 定义jackson对象
*/
private static final ObjectMapper MAPPER = new ObjectMapper();

/**
* 将对象转换成json字符串。
* <p>Title: pojoToJson</p>
* <p>Description: </p>
* @param data
* @return
*/
public static String toJsonStr(Object data) {
try {
// ObjectWriter writer = MAPPER.writer();
// String string = writer.writeValueAsString(data);
String string = MAPPER.writeValueAsString(data);
return string;
} catch (JsonProcessingException e) {
e.printStackTrace();
}
return null;
}

/**
* 将json结果集转化为对象
*
* @param jsonData json数据
* @param beanType 对象中的object类型
* @return
*/
public static <T> T toPojo(String jsonData, Class<T> beanType) {
try {
T t = MAPPER.readValue(jsonData, beanType);
return t;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

/**
* 将json数据转换成pojo对象list
* <p>Title: jsonToList</p>
* <p>Description: </p>
* @param jsonData
* @param beanType
* @return
*/
public static <T> List<T> jsonToList(String jsonData, Class<T> beanType) {
JavaType javaType = MAPPER.getTypeFactory().constructParametricType(List.class, beanType);
try {
List<T> list = MAPPER.readValue(jsonData, javaType);
return list;
} catch (Exception e) {
e.printStackTrace();
}

return null;
}

/**
* 获取ObjectMapper
* @return
*/
public static ObjectMapper getObjectMapper() {
return JsonUtil.MAPPER;
}

}

 

 

package com.guanwei.study.json;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gw.common.utils.JsonUtil;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;

/**
* @Title: 测试JSON性能
* @Description: jacksonUtil 胜出
* @Version: v1.0
* @Author: Mr.Guan
* @Mail GuanWeiMail@163.com
* @DateTime: 2021-02-24
* @Project springcloud-gw-parent
* @Package com.guanwei.study.json
*/
public class JsonTest {

/**
* 多线程数量
*/
private static final int mulThreadCount = 1000;

private static final int count = 100000;


/**
* Spring 中默认用到的是Jackson
* jacksonUtil 胜出
* @param str
*/
public static void main(String [] str) {

Map map = new HashMap<String,Object>(1000000);
map = getMapData(map,1000000);

int count = JsonTest.count;

//hutoolJson
long hutoolJsonBegin = System.currentTimeMillis();
JSONUtil.toJsonStr(map);
for (int i = 0; i < count; i++) {
JSONUtil.toJsonStr(new HashMap<>());
}
long hutoolJsonEnd = System.currentTimeMillis();
System.out.println("hutoolJson-------------time------------------------------------"+(hutoolJsonEnd-hutoolJsonBegin));


//jackson
long jacksonBegin = System.currentTimeMillis();
ObjectMapper jackson = new ObjectMapper();
try {
jackson.writeValueAsString(map);
for (int i = 0; i < count; i++) {
jackson.writeValueAsString(new HashMap<>());
}
long jacksonEnd = System.currentTimeMillis();
System.out.println("jackson-------------time------------------------------------"+(jacksonEnd-jacksonBegin));
} catch (JsonProcessingException e) {
e.printStackTrace();
}

//jackson工具 (推荐 速度最快)
long jacksonBegin1 = System.currentTimeMillis();
JsonUtil.toJsonStr(map);
for (int i = 0; i < count; i++) {
JsonUtil.toJsonStr(new HashMap<>());
}
long jacksonEnd1 = System.currentTimeMillis();
System.out.println("jacksonUtil-------------time------------------------------------"+(jacksonEnd1-jacksonBegin1));


//fastJson
long fastJsonBegin = System.currentTimeMillis();
String s = JSONObject.toJSONString(map);
for (int i = 0; i < count; i++) {
JSONObject.toJSONString(new HashMap<>());
}
long fastJsonEnd = System.currentTimeMillis();
System.out.println("fastJson-------------time------------------------------------"+(fastJsonEnd-fastJsonBegin));

JsonTest.testMulThreadFastJson();
JsonTest.testMulThreadJacksonUtil();

}


/**
* 多线程模式jacksonUtil
*
* @throws InterruptedException
*/
public static void testMulThreadJacksonUtil() {
CountDownLatch countDownLatch = new CountDownLatch(mulThreadCount);
long start = System.currentTimeMillis();
for (int i = 0; i < mulThreadCount; i++) {
int finalI = i;
new Thread(() -> {
JsonUtil.toJsonStr(new HashMap<>(1));
countDownLatch.countDown();
}).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("jacksonUtil 多线程模式最终用时:" + (System.currentTimeMillis() - start) + "ms");
}

/**
* 多线程模式FastJson
*
* @throws InterruptedException
*/
public static void testMulThreadFastJson() {
CountDownLatch countDownLatch = new CountDownLatch(mulThreadCount);
long start = System.currentTimeMillis();
for (int i = 0; i < mulThreadCount; i++) {
int finalI = i;
new Thread(() -> {
JSONObject.toJSONString(new HashMap<>(1));
countDownLatch.countDown();
}).start();
}
try {
countDownLatch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("FastJson 多线程模式最终用时:" + (System.currentTimeMillis() - start) + "ms");
}


/**
* 生成指定数量的map
* @param map
* @param count
* @return
*/
public static Map getMapData(Map map,int count) {
for (int i = 0;i < count;i++) {
map.put("a"+i,i);
}
return map;
}

}