Categorygithub.com/zhangzhanli/gojson
repositorypackage
1.0.5
Repository: https://github.com/zhangzhanli/gojson.git
Documentation: pkg.go.dev

# README

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

gojson114658 ns/op457.72 MB/s5873 B/op253 allocs/op
jsonparser116647 ns/op449.92 MB/s0 B/op0 allocs/op
easyjson219673 ns/op238.91 MB/s9264 B/op349 allocs/op
gojay92398 ns/op567.99 MB/s5985 B/op259 allocs/op
jsoniter159877 ns/op328.26 MB/s800 B/op29 allocs/op
encoding/json680867 ns/op377.08 MB/s6154 B/op258 allocs/op

Marshal

gojson131 ns/op400511.56 MB/s32 B/op1 allocs/op
easyjson235 ns/op223039.53 MB/s272 B/op3 allocs/op
encoding/json597 ns/op87819.16 MB/s120 B/op4 allocs/op

Medium Payload

Unmarshal

gojson4056 ns/op510.51 MB/s240 B/op10 allocs/op
jsonparser9379 ns/op220.81 MB/s0 B/op0 allocs/op
easyjson8107 ns/op255.43 MB/s288 B/op11 allocs/op
gojay5004 ns/op413.84 MB/s2472 B/op10 allocs/op
jsoniter10579 ns/op195.75 MB/s2576 B/op12 allocs/op
encoding/json28030 ns/op73.88 MB/s520 B/op15 allocs/op

Marshal

gojson105 ns/op19551.00 MB/s32 B/op1 allocs/op
easyjson193 ns/op10700.99 MB/s272 B/op3 allocs/op
encoding/json443 ns/op4667.11 MB/s120 B/op4 allocs/op

Small Payload

Unmarshal

gojson812 ns/op176.00 MB/s160 B/op5 allocs/op
jsonparser925 ns/op154.47 MB/s0 B/op0 allocs/op
easyjson1209 ns/op118.26 MB/s128 B/op8 allocs/op
gojay977 ns/op146.30 MB/s304 B/op6 allocs/op
jsoniter1847 ns/op77.42 MB/s336 B/op7 allocs/op
encoding/json2576 ns/op55.49 MB/s328 B/op7 allocs/op

Marshal

gojson331 ns/op431.90 MB/s32 B/op1 allocs/op
easyjson439 ns/op325.35 MB/s272 B/op3 allocs/op
encoding/json1398 ns/op102.22 MB/s120 B/op4 allocs/op

Questions

Any questions or bugs can go though github issues.