知行编程网知行编程网  2022-05-24 10:00 知行编程网 隐藏边栏 |   抢沙发  69 
文章评分 0 次,平均分 0.0
点击上方“菜鸟学Python”,选择“星标

超级无敌干货第一时间推给你!


菜鸟学点Java,这么多牛逼的Java常用Json库,没想到它最好!


本篇通过JMH来测试一下Java中几种常见的JSON解析库的性能。每次都在网上看到别人说什么某某库性能是如何如何的好,碾压其他的库。但是百闻不如一见,只有自己亲手测试过的才是最值得相信的。

JSON不管是在Web开发还是服务器开发中是相当常见的数据传输格式,一般情况我们对于JSON解析构造的性能并不需要过于关心,除非是在性能要求比较高的系统。

目前对于Java开源的JSON类库有很多种,下面我们取4个常用的JSON库进行性能测试对比, 同时根据测试结果分析如果根据实际应用场景选择最合适的JSON库。

这4个JSON类库分别为:Gson,FastJson,Jackson,Json-lib。

简单介绍

选择一个合适的JSON库要从多个方面进行考虑:

  • 字符串解析成JSON性能

  • 字符串解析成JavaBean性能

  • JavaBean构造JSON性能

  • 集合构造JSON性能

  • 易用性

先简单介绍下四个类库的身份背景。

Gson

项目地址:https://github.com/google/gson

Gson是目前功能最全的Json解析神器,Gson当初是为因应Google公司内部需求而由Google自行研发而来,但自从在2008年五月公开发布第一版后已被许多公司或用户应用。

Gson的应用主要为toJson与fromJson两个转换函数,无依赖,不需要例外额外的jar,能够直接跑在JDK上。

在使用这种对象转换之前,需先创建好对象的类型以及其成员才能成功的将JSON字符串成功转换成相对应的对象。类里面只要有get和set方法,Gson完全可以实现复杂类型的json到bean或bean到json的转换,是JSON解析的神器。

FastJson

项目地址:https://github.com/alibaba/fastjson

Fastjson是一个Java语言编写的高性能的JSON处理器,由阿里巴巴公司开发。无依赖,不需要例外额外的jar,能够直接跑在JDK上。

FastJson在复杂类型的Bean转换Json上会出现一些问题,可能会出现引用的类型,导致Json转换出错,需要制定引用。FastJson采用独创的算法,将parse的速度提升到极致,超过所有json库。

Jackson

项目地址:https://github.com/FasterXML/jackson

Jackson是当前用的比较广泛的,用来序列化和反序列化json的Java开源框架。

Jackson社区相对比较活跃,更新速度也比较快, 从Github中的统计来看,Jackson是最流行的json解析器之一,Spring MVC的默认json解析器便是Jackson。

Jackson优点很多:

  • Jackson 所依赖的jar包较少,简单易用。

  • 与其他 Java 的 json 的框架 Gson 等相比,Jackson 解析大的 json 文件速度比较快。

  • Jackson 运行时占用内存比较低,性能比较好

  • Jackson 有灵活的 API,可以很容易进行扩展和定制。

目前最新版本是2.9.4,Jackson 的核心模块由三部分组成:

  • jackson-core 核心包,提供基于”流模式”解析的相关 API,它包括 JsonPaser 和 JsonGenerator。Jackson 内部实现正是通过高性能的流模式 API 的 JsonGenerator 和 JsonParser 来生成和解析 json。

  • jackson-annotations 注解包,提供标准注解功能;

  • jackson-databind 数据绑定包,提供基于”对象绑定” 解析的相关 API( ObjectMapper )和”树模型” 解析的相关 API(JsonNode);基于”对象绑定” 解析的 API 和”树模型”解析的 API 依赖基于”流模式”解析的 API。

Json-lib

项目地址:http://json-lib.sourceforge.net/index.html

json-lib最开始的也是应用最广泛的json解析工具,json-lib 不好的地方确实是依赖于很多第三方包,对于复杂类型的转换,json-lib对于json转换成bean还有缺陷, 比如一个类里面会出现另一个类的list或者map集合,json-lib从json到bean的转换就会出现问题。json-lib在功能和性能上面都不能满足现在互联网化的需求。

编写性能测试

接下来开始编写这四个库的性能测试代码。

添加maven依赖

当然首先是添加四个库的maven依赖,公平起见,我全部使用它们最新的版本:

四个库的工具类

FastJsonUtil.java

GsonUtil.java

JacksonUtil.java

JsonLibUtil.java

准备Model类

这里我写一个简单的Person类,同时属性有Date、List、Map和自定义的类FullName,最大程度模拟真实场景。


JSON序列化性能基准测试


说明一下,上面的代码中



这个是我自己编写的将性能测试报告数据填充至Echarts图,然后导出png图片的方法。

执行后的结果图:

菜鸟学点Java,这么多牛逼的Java常用Json库,没想到它最好!从上面的测试结果可以看出,序列化次数比较小的时候,Gson性能最好,当不断增加的时候到了100000,Gson明细弱于Jackson和FastJson, 这时候FastJson性能是真的牛,另外还可以看到不管数量少还是多,Jackson一直表现优异。而那个Json-lib简直就是来搞笑的。^_^

JSON反序列化性能基准测试


执行后的结果图:

菜鸟学点Java,这么多牛逼的Java常用Json库,没想到它最好!从上面的测试结果可以看出,反序列化的时候,Gson、Jackson和FastJson区别不大,性能都很优异,而那个Json-lib还是来继续搞笑的。

以上就是几种几种主流JSON库的基本介绍,希望能对你有所帮助!

作者:飞污熊

来源:https://urlify.cn/EJV73y

<pre style="letter-spacing: 0.544px;"><pre style=""><section data-mpa-template="t" mpa-from-tpl="t" style=""><section style="font-family: -apple-system-font, system-ui, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing: 0.544px;text-align: right;white-space: normal;"><section mpa-from-tpl="t" style=""><section data-tools="135编辑器" data-id="94301" style=""><section style=""><section style=""><section data-tools="135编辑器" data-id="93743" style=""><section style=""><section style="margin-bottom: 16px;transition: margin 0.1s linear 0s, padding 0.1s linear 0s, width 0.1s linear 0s, height 0.1s linear 0s;letter-spacing: 0.5px;line-height: 1.5em;font-family: 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei', 'WenQuanYi Micro Hei', 'Helvetica Neue', Arial, sans-serif;"><section data-tools="135编辑器" data-id="93743" style="letter-spacing: 0.544px;font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;"><pre data-darkmode-color="rgb(168, 168, 168)" data-style="padding-right: 0.5em; padding-left: 0.5em; max-width: 100%; background-color: rgb(255, 255, 255); color: rgb(62, 62, 62); letter-spacing: 0.544px; font-size: 15px; text-align: start; word-spacing: 2px; box-sizing: border-box !important; overflow-wrap: break-word !important;" data-darkmode-bgcolor="rgb(36, 36, 36)" style="text-align: left;"><section data-role="outer" label="Powered by 135editor.com" style="color: rgb(154, 154, 154);font-size: 16px;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, Arial, sans-serif;text-align: center;"><section style="margin: 15px 16px 1em;min-height: 1em;white-space: pre-wrap;width: inherit;font-family: Avenir, PingFangTC-Light !important;line-height: 1.8em !important;color: rgb(79, 79, 79) !important;"><span style="color: rgb(178, 178, 178);font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;font-size: 14px;letter-spacing: 0.544px;">--END--</span></section></section><section data-role="outer" label="Powered by 135editor.com" style="color: rgb(154, 154, 154);font-size: 16px;letter-spacing: 0.544px;white-space: normal;font-family: -apple-system-font, system-ui, Arial, sans-serif;text-align: center;"><section data-tools="135编辑器" data-id="93529"><section style="margin-right: 16px;margin-left: 16px;"><span data-role="width" data-width="80%" style="display: inline-block;width: 516px;"><img data-ratio="0.08658008658008658" data-type="gif" data-w="462" data-width="80%" title="" class="__bg_gif" width="80%" height="" border="0"  style="box-sizing: border-box !important;visibility: visible !important;width: 412.797px !important;" src="https://www.zkxjob.com/wp-content/uploads/2022/05/wxsync-2022-05-a55c1c790f36cb0674b2473e5d4e4666.gif"  /></span></section></section></section><p style="color: rgb(154, 154, 154);font-size: 16px;font-family: -apple-system-font, system-ui, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;"><span style="color: rgb(87, 107, 149);font-family: -apple-system-font, BlinkMacSystemFont, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;font-size: 15px;letter-spacing: 0.544px;"></span><br  /></p><section style="text-align: left;line-height: 2em;"><span style="color: rgb(87, 107, 149);font-size: 15px;"></span></section><pre data-darkmode-color="rgb(168, 168, 168)" data-style="padding-right: 0.5em; padding-left: 0.5em; max-width: 100%; background-color: rgb(255, 255, 255); color: rgb(62, 62, 62); letter-spacing: 0.544px; font-size: 15px; text-align: start; word-spacing: 2px; box-sizing: border-box !important; overflow-wrap: break-word !important;" data-darkmode-bgcolor="rgb(36, 36, 36)" style="letter-spacing: 0.544px;color: rgb(154, 154, 154);font-size: 16px;text-align: -webkit-center;widows: 1;word-spacing: 2px;caret-color: rgb(255, 0, 0);background-color: rgb(255, 255, 255);"><section><section data-tool="mdnice编辑器" data-website="https://www.mdnice.com" style="margin-top: -10px;padding-right: 10px;padding-left: 10px;text-align: left;font-family: Optima-Regular, Optima, PingFangSC-light, PingFangTC-light, 'PingFang SC', Cambria, Cochin, Georgia, Times, 'Times New Roman', serif;line-height: 1.6;letter-spacing: 0.034em;color: rgb(63, 63, 63);"><pre style="text-size-adjust: auto;letter-spacing: 0.544px;word-spacing: 1px;line-height: inherit;"><p style="line-height: 1.8em;orphans: 4;font-size: 15px;letter-spacing: 0.1em;white-space: pre-wrap;font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;text-align: justify;margin: 2em 5px !important;"><span style="color: rgb(0, 122, 170);"><strong>Java遇见PythongGo</strong></span>,现已正式上线!<br  /></p><p style="line-height: 1.8em;orphans: 4;font-size: 15px;letter-spacing: 0.1em;white-space: pre-wrap;font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;text-align: justify;margin: 2em 5px !important;">接下来我们将会在该上,为大家<span style="color: rgb(0, 122, 170);"><strong>分享优质的 Java,Python,Go领域干货</strong></span>,不限于 BAT 面试, 算法,数据库,微服务,高并发, Python数据分析,自动化运维,机器学习等相关知识,期待与您一同进步。</p><section style="margin-right: 8px;margin-left: 8px;padding-right: 0.5em;padding-left: 0.5em;font-family: -apple-system-font, system-ui, 'Helvetica Neue', 'PingFang SC', 'Hiragino Sans GB', 'Microsoft YaHei UI', 'Microsoft YaHei', Arial, sans-serif;letter-spacing: 0.544px;white-space: normal;text-align: center;"></section>

回复”福利“获取新整理的精华资料

本篇文章来源于: 菜鸟学Python

本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

知行编程网
知行编程网 关注:1    粉丝:1
这个人很懒,什么都没写

你可能也喜欢

热评文章

发表评论

表情 格式 链接 私密 签到
扫一扫二维码分享