博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
dslJson、jsoniter、fastjson、jackson解析字符串为map效率比较
阅读量:6939 次
发布时间:2019-06-27

本文共 4035 字,大约阅读时间需要 13 分钟。

hot3.png

package test;import cn.hutool.core.lang.Console;import cn.jiguang.entity.ErrorMessage;import com.alibaba.fastjson.JSON;import com.dslplatform.json.DslJson;import com.dslplatform.json.JsonReader;import com.fasterxml.jackson.databind.ObjectMapper;import com.jsoniter.JsonIterator;import com.jsoniter.ValueType;import org.junit.Before;import org.junit.Test;import org.springframework.util.Assert;import java.io.*;import java.util.Map;import java.util.concurrent.atomic.AtomicReference;/** * @desc: dsl-json  fastjson  jackson  jsoniter 效率比较 * @author: zengxc * @date: 2018/4/4 */public class JSONTest {    private static final ObjectMapper MAPPER = new ObjectMapper();    public static String[] factors = {};    private static String JSON_STR = null;    private static int count = 0;    private final DslJson dslJson = new DslJson();    @Test    public void testJsoniter2() throws IOException {        JsonIterator jsonIterator = JsonIterator.parse("[123,{'id':20, 'errorMsg':'this error'},456]".replace('\'', '"'));        AtomicReference
errorMessage = new AtomicReference<>(); jsonIterator.readAny().forEach(any -> { if (any.valueType() == ValueType.OBJECT) { errorMessage.set(any.as(ErrorMessage.class)); } }); Console.print(errorMessage.get()); Assert.isTrue(true, "Not to false"); } @Test public void testJsoniter() throws IOException { JsonIterator jsonIterator = JsonIterator.parse("{'id':10, 'errorMsg':'this error'}".replace('\'', '"')); ErrorMessage errorMessage = jsonIterator.read(ErrorMessage.class); Assert.notNull(errorMessage, "can't be empty "); } @Test public void testFor() throws IOException { File file = new File("out.txt"); file.mkdirs(); PrintWriter printWriter = new PrintWriter(file); StringBuffer buffer = new StringBuffer(); buffer.append("{"); for (int i = 0; i < 1000000; i++) { if (i == 999999) { buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + "}"); break; } buffer.append("\"k" + i + "\":" + "\"v" + i + "\"" + ","); } printWriter.println(buffer.toString()); printWriter.flush(); } @Before public void init() throws Exception { FileReader fis = new FileReader(new File("D:\\WorkSpace\\springboot-helloworld\\out.txt")); BufferedReader br = new BufferedReader(fis); JSON_STR = br.readLine(); } @Test public void testFastJson() { long startTime = System.currentTimeMillis(); Map
map = JSON.parseObject(JSON_STR, Map.class); map.forEach((k, v) -> { count++; }); System.out.println(System.currentTimeMillis() - startTime); System.out.println(count); } @Test public void testJsoniter03() throws IOException { long startTime = System.currentTimeMillis(); JsonIterator jsonIterator = JsonIterator.parse(JSON_STR); Map
map = jsonIterator.readAny().as(Map.class); map.forEach((k, v) -> { count++; }); System.out.println(System.currentTimeMillis() - startTime); System.out.println(count); } @Test public void testJackson() throws IOException { long startTime = System.currentTimeMillis(); Map
map = MAPPER.readValue(JSON_STR, Map.class); map.forEach((k, v) -> { count++; }); System.out.println(System.currentTimeMillis() - startTime); System.out.println(count); } @Test public void testDslJson() throws IOException { long startTime = System.currentTimeMillis(); final byte[] buff = JSON_STR.getBytes("UTF-8"); JsonReader
jsonReader = dslJson.newReader(buff); Map map = jsonReader.next(Map.class); map.forEach((k, v) -> { count++; }); System.out.println(System.currentTimeMillis() - startTime); System.out.println(count); }}

结论:

dslJson > jsonIter > fastJson > jackson

转载于:https://my.oschina.net/u/3744350/blog/1790116

你可能感兴趣的文章
OSChina 周四乱弹 ——程序员座驾千万,出门千人陪同
查看>>
使用video标签或者videoJS 做 mp4点播 编码不对播放失败。
查看>>
SSH公钥生成及配置
查看>>
iOS 新浪微博快速集成
查看>>
禁用浏览器返回
查看>>
flask+uwsgi+nginx部署网站
查看>>
从九寨沟地震 细数那些数据中心受过的伤害
查看>>
BIPlatform的安装以及本地开发环境搭建
查看>>
GOF23之适配器模式
查看>>
PHP跨页面SESSION丢失问题
查看>>
Java Socket探究
查看>>
现在在下载Google APIs by Google Inc. 速度为1kib/s,特此纪念又...
查看>>
Android Push开源解决方案
查看>>
安卓constraintLayout中app:srcCompat设置的图片显示不出来
查看>>
webmagic新版文档(更新中)
查看>>
nginx 安装在ubuntu上
查看>>
Xtrabackup全量备份/增量备份脚本
查看>>
白天谋生存,晚上谋发展
查看>>
hive 12及以后,可以使用非同步查询
查看>>
CentOS 7安装部署ELK 6.2.4
查看>>