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
.
# install
go get -u -v github.com/go-fish/gojson
cd ${GOPATH}/src/github.com/go-fish/gojson/cmd/gojson
go build -o gojson main.go
# usage
gojson [flags] [file|directory]...
Flags:
--escapehtml escape html char when encode object to json
--escapeunicode escape unicode rune when decode object to json
-h, --help help for gojson
-o, --output string the filename of output file (default "gojson.generated.go")
--unsafe use input data directly without copy when decode json bytes (default true)
- For expose structs, gojson will generate
MarshalJSON/UnmarshalJSON
methods for marshal/unmarshal json. You also can usegojson.Marshal/gojson.Unmarshal
functions to marshal/unmarshal json. - gojson will generate Marshal and Unmarsnal code by default, if you don't want to generate code for struct, use comment
+gojson:noencode
,+gojson:nodecode
or+gojson:none
. - If your field contains many escaped char, add
escape
to your field tag, it will improve preformance when unmarshal escape string.
gojson | 91022 ns/op | 576.61 MB/s | 62393 B/op | 200 allocs/op |
---|---|---|---|---|
jsonparser | 91804 ns/op | 571.70 MB/s | 57344 B/op | 1 allocs/op |
easyjson | 157800 ns/op | 332.60 MB/s | 65388 B/op | 286 allocs/op |
gojay | 84526 ns/op | 620.92 MB/s | 62393 B/op | 200 allocs/op |
jsoniter | 142165 ns/op | 369.18 MB/s | 81216 B/op | 2162 allocs/op |
encoding/json | 437117 ns/op | 120.07 MB/s | 62657 B/op | 205 allocs/op |
gojson | 238.5 ns/op | 220076.86 MB/s | 1048 B/op | 2 allocs/op |
---|---|---|---|---|
easyjson | 235 ns/op | 223039.53 MB/s | 272 B/op | 3 allocs/op |
encoding/json | 1232 ns/op | 42610.66 MB/s | 1369 B/op | 10 allocs/op |
gojson | 3649 ns/op | 592.74 MB/s | 2457 B/op | 10 allocs/op |
---|---|---|---|---|
jsonparser | 6861 ns/op | 315.25 MB/s | 2304 B/op | 1 allocs/op |
easyjson | 8951 ns/op | 241.64 MB/s | 2569 B/op | 11 allocs/op |
gojay | 4145 ns/op | 521.79 MB/s | 2472 B/op | 9 allocs/op |
jsoniter | 14565 ns/op | 148.51 MB/s | 5340 B/op | 86 allocs/op |
encoding/json | 28891 ns/op | 74.87 MB/s | 2721 B/op | 15 allocs/op |
gojson | 282.5 ns/op | 7656.48 MB/s | 1048 B/op | 2 allocs/op |
---|---|---|---|---|
easyjson | 155.3 ns/op | 13926.91 MB/s | 264 B/op | 3 allocs/op |
encoding/json | 1177 ns/op | 1837.14 MB/s | 1369 B/op | 10 allocs/op |
gojson | 598.3 ns/op | 239.01 MB/s | 296 B/op | 5 allocs/op |
---|---|---|---|---|
jsonparser | 701.1 ns/op | 203.97 MB/s | 144 B/op | 1 allocs/op |
easyjson | 1058 ns/op | 135.19 MB/s | 272 B/op | 8 allocs/op |
gojay | 606.3 ns/op | 235.87 MB/s | 296 B/op | 5 allocs/op |
jsoniter | 1427 ns/op | 100.22 MB/s | 504 B/op | 16 allocs/op |
encoding/json | 1873 ns/op | 76.35 MB/s | 448 B/op | 7 allocs/op |
gojson | 388.1 ns/op | 368.49 MB/s | 1048 B/op | 2 allocs/op |
---|---|---|---|---|
easyjson | 288.2 ns/op | 496.20 MB/s | 264 B/op | 3 allocs/op |
encoding/json | 1009 ns/op | 141.75 MB/s | 1129 B/op | 4 allocs/op |
Any questions or bugs can go though github issues.