Json 做为一种重要的数据格式,具备良好的可读性以及自描述性,普遍地应用在各类数据传输场景中。Go 语言里面原生支持了这种数据格式的序列化以及反序列化,内部使用反射机制实现,性能有点差,在高度依赖 json 解析的应用里,每每会成为性能瓶颈,好在已有不少第三方库帮咱们解决了这个问题,可是这么多库,对于像我这种有选择困难症的人来讲,到底要怎么选择呢,下面就给你们来一一分析一下node
go get -u github.com/pquerna/ffjson
原生的库性能比较差的主要缘由是使用了不少反射的机制,为了解决这个问题,ffjson 经过预编译生成代码,类型的判断在预编译阶段已经肯定,避免了在运行时的反射git
但也所以在编译前须要多一个步骤,须要先生成 ffjson 代码,生成代码只须要执行 ffjson <file.go>
就能够了,其中 file.go
是一个包含 json 结构体定义的 go 文件。注意这里 ffjson 是这个库提供的一个代码生成工具,直接执行上面的 go get
会把这个工具安装在 $GOPATH/bin
目录下,把 $GOPATH/bin
加到 $PATH
环境变量里面,能够全局访问github
另外,若是有些结构,不想让 ffjson 生成代码,能够经过增长注释的方式golang
// ffjson: skip type Foo struct { Bar string } // ffjson: nodecoder type Foo struct { Bar string }
go get -u github.com/mailru/easyjson/...
easyjson 的思想和 ffjson 是一致的,都是增长一个预编译的过程,预先生成对应结构的序列化反序列化代码,除此以外,easyjson 还放弃了一些原生库里面支持的一些没必要要的特性,好比:key 类型声明,key 大小写不敏感等等,以达到更高的性能json
生成代码执行 easyjson -all <file.go>
便可,若是不指定 -all
参数,只会对带有 //easyjson:json
的结构生成代码工具
//easyjson:json type A struct { Bar string }
go get -u github.com/json-iterator/go
这是一个很神奇的库,滴滴开发的,不像 easyjson 和 ffjson 都使用了预编译,并且 100% 兼容原生库,可是性能超级好,也不知道怎么实现的,若是有人知道的话,能够告诉我一下吗?性能
使用上面,你只要把全部的测试
import "encoding/json"
替换成jsonp
import "github.com/json-iterator/go" var json = jsoniter.ConfigCompatibleWithStandardLibrary
就能够了,其它都不须要动spa
go get -u github.com/ugorji/go/codec
这个库里面其实包含不少内容,json 只是其中的一个功能,比较老,使用起来比较麻烦,性能也不是很好
go get -u github.com/buger/jsonparser
严格来讲,这个库不属于 json 序列化的库,只是提供了一些 json 解析的接口,使用的时候须要本身去设置结构里面的值,事实上,每次调用都须要从新解析 json 对象,性能并非很好
就像名字暗示的那样,这个库只是一个解析库,并无序列化的接口
对上面这些 json 库,做了一些性能测试,测试代码在:https://github.com/hatlonely/...,下面是在个人 Macbook 上测试的结果(实际结果和库的版本以及机器环境有关,建议本身再测试一遍):
BenchmarkMarshalStdJson-4 1000000 1097 ns/op BenchmarkMarshalJsonIterator-4 2000000 781 ns/op BenchmarkMarshalFfjson-4 2000000 941 ns/op BenchmarkMarshalEasyjson-4 3000000 513 ns/op BenchmarkMarshalCodecJson-4 1000000 1074 ns/op BenchmarkMarshalCodecJsonWithBufio-4 1000000 2161 ns/op BenchmarkUnMarshalStdJson-4 500000 2512 ns/op BenchmarkUnMarshalJsonIterator-4 2000000 591 ns/op BenchmarkUnMarshalFfjson-4 1000000 1127 ns/op BenchmarkUnMarshalEasyjson-4 2000000 608 ns/op BenchmarkUnMarshalCodecJson-4 20000 122694 ns/op BenchmarkUnMarshalCodecJsonWithBufio-4 500000 3417 ns/op BenchmarkUnMarshalJsonparser-4 2000000 877 ns/op
从上面的结果能够看出来:
因此综合考虑,建议你们使用 jsoniter,若是追求极致的性能,考虑 easyjson
ffjson: https://github.com/pquerna/ff...
easyjson: https://github.com/mailru/eas...
jsoniter: https://github.com/json-itera...
jsonparser: https://github.com/buger/json...
codecjson: http://ugorji.net/blog/go-cod...
转载请注明出处
本文连接: http://hatlonely.github.io/20...