推荐一个Json解析库

栏目: 后端 · 前端 · 发布时间: 5年前

内容简介:推荐一个json库with -inline && -unsafe, you can get most performance generate code with -m, gojson will generate marshal/unmarshal/both for all expose structs in input dir/file.

推荐一个json库 gojson ,可以通过命令行 工具 根据输入的Struct动态生成 MarshalJSON/UnmarshalJSON 方法,性能与EasyJSON、Jsonparser、Jsoniter相比要好很多

GoJSON

GoJSON is a fast and easy package to marshal/unmarshal struct to/from json. You can use GoJSON tool to generate marshal/unmarshal code, in benchmark tests, GoJSON 's generate code is almost 6~7 times faster than encoding/json .

Example

# install
  go get -u -v github.com/go-fish/gojson
  cd ${GOPATH}/src/github.com/go-fish/gojson/cmd
  go build -o gojson main.go


# usage
  gojson [options] <input dir|file>
  
  -inline
        Use inline function in generate code (default true)
  -m string
        Mode of generate, eg: encode, decode, all (default "all")
  -o string
        Optional name of the output file to be generated. (default "gojson.generate.go")
  -unsafe
        Use decoder without copy data
  -version
        Show version information.

with -inline && -unsafe, you can get most performance generate code with -m, gojson will generate marshal/unmarshal/both for all expose structs in input dir/file.

For expose structs, gojson generate MarshalJSON/UnmarshalJSON methods for marshal/unmarshal json. You also can use gojson.Marshal/gojson.Unmarshal functions to marshal/unmarshal json.

Benchmark

Large Payload

Unmarshal

gojson 114658 ns/op 457.72 MB/s 5873 B/op 253 allocs/op
jsonparser 116647 ns/op 449.92 MB/s 0 B/op 0 allocs/op
easyjson 219673 ns/op 238.91 MB/s 9264 B/op 349 allocs/op
gojay 92398 ns/op 567.99 MB/s 5985 B/op 259 allocs/op
jsoniter 159877 ns/op 328.26 MB/s 800 B/op 29 allocs/op
encoding/json 680867 ns/op 377.08 MB/s 6154 B/op 258 allocs/op

Marshal

gojson 131 ns/op 400511.56 MB/s 32 B/op 1 allocs/op
easyjson 235 ns/op 223039.53 MB/s 272 B/op 3 allocs/op
encoding/json 597 ns/op 87819.16 MB/s 120 B/op 4 allocs/op

Medium Payload

Unmarshal

gojson 4056 ns/op 510.51 MB/s 240 B/op 10 allocs/op
jsonparser 9379 ns/op 220.81 MB/s 0 B/op 0 allocs/op
easyjson 8107 ns/op 255.43 MB/s 288 B/op 11 allocs/op
gojay 5004 ns/op 413.84 MB/s 2472 B/op 10 allocs/op
jsoniter 10579 ns/op 195.75 MB/s 2576 B/op 12 allocs/op
encoding/json 28030 ns/op 73.88 MB/s 520 B/op 15 allocs/op

Marshal

gojson 105 ns/op 19551.00 MB/s 32 B/op 1 allocs/op
easyjson 193 ns/op 10700.99 MB/s 272 B/op 3 allocs/op
encoding/json 443 ns/op 4667.11 MB/s 120 B/op 4 allocs/op

Small Payload

Unmarshal

gojson 812 ns/op 176.00 MB/s 160 B/op 5 allocs/op
jsonparser 925 ns/op 154.47 MB/s 0 B/op 0 allocs/op
easyjson 1209 ns/op 118.26 MB/s 128 B/op 8 allocs/op
gojay 977 ns/op 146.30 MB/s 304 B/op 6 allocs/op
jsoniter 1847 ns/op 77.42 MB/s 336 B/op 7 allocs/op
encoding/json 2576 ns/op 55.49 MB/s 328 B/op 7 allocs/op

Marshal

gojson 331 ns/op 431.90 MB/s 32 B/op 1 allocs/op
easyjson 439 ns/op 325.35 MB/s 272 B/op 3 allocs/op
encoding/json 1398 ns/op 102.22 MB/s 120 B/op 4 allocs/op

Questions

Any questions or bugs can go though github issues.


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

啊哈C!思考快你一步

啊哈C!思考快你一步

啊哈磊 / 电子工业出版社 / 2013-9 / 39.00元

这是一本非常有趣的编程启蒙书,全书从中小学生的角度来讲述,没有生涩的内容,取而代之的是生动活泼的漫画和风趣幽默的文字。并配合超萌的编程软件,从开始学习与计算机对话到自己独立制作一个游戏,由浅入深地讲述编程的思维。同时,与计算机展开的逻辑较量一定会让你觉得很有意思。你可以在茶余饭后阅读本书,甚至蹲在马桶上时也可以看得津津有味。编程将会改变我们的思维,教会我们如何思考,让我们的思维插上计算机的翅膀,以......一起来看看 《啊哈C!思考快你一步》 这本书的介绍吧!

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具