encoding/json
encoding/jsonomitempty
Usage
# install
go get -u github.com/mailru/easyjson/...
# run
easyjson -all <file>.go
_easyjson.go.go
GOPATHgo run
Options
Usage of easyjson:
-all
generate marshaler/unmarshalers for all structs in a file
-build_tags string
build tags to add to generated file
-gen_build_flags string
build flags when running the generator while bootstrapping
-byte
use simple bytes instead of Base64Bytes for slice of bytes
-leave_temps
do not delete temporary files
-no_std_marshalers
don't generate MarshalJSON/UnmarshalJSON funcs
-noformat
do not run 'gofmt -w' on output file
-omit_empty
omit empty fields by default
-output_filename string
specify the filename of the output
-pkg
process the whole package instead of just the given file
-snake_case
use snake_case names instead of CamelCase by default
-lower_camel_case
use lowerCamelCase instead of CamelCase by default
-stubs
only generate stubs for marshaler/unmarshaler funcs
-disallow_unknown_fields
return error if some unknown field in json appeared
-disable_members_unescape
disable unescaping of \uXXXX string sequences in member names
-alleasyjson:skip
//easyjson:skip
type A struct {}
-alleasyjson:json
//easyjson:json
type A struct {}
附加选项注释:
-snake_case-build_tags-gen_build_flags-gen_build_flags="-mod=mod -x"
结构json标记选项
除了standart json标记选项(如“omitempty”)外,还支持以下选项:
- “nocopy”-禁用字符串值的分配和复制,使它们引用原始json缓冲区内存。对于解码和立即使用后不保存在内存中的短生命对象来说,这非常有用。注意,如果字符串需要取消转义,它将按正常方式处理。
- ‘intern’-字符串“interning”(重复数据消除),当整个结构中经常遇到相同的字符串字典值时,可以节省内存。详见下文。
Generated Marshaler/Unmarshaler Funcs
MarshalEasyJSONUnmarshalEasyJSONeasyjson.Marshalereasyjson.Unmarshalereasyjson.Marshaleasyjson.Unmarshal
json.Marshalerjson.UnmarshalerMarshalJSONUnmarshalJSONeasyjson.Marshaleasyjson.Unmarshaljson.Marshaljson.Unmarshal
MarshalEasyJSONUnmarshalEasyJSONeasyjson.MarshalToHTTPResponseWriterhttp.ResponseWriter
控制easyjson封送和解组行为
MarshalEasyJSONUnmarshalEasyJSONeasyjson.Marshalereasyjson.Unmarshalereasyjson.Marshaleasyjson.Unmarshal
easyjson.Optionalomitempty
Type Wrappers
easyjson/opt
easyjson/opt
Memory Pooling
sync.Pool
easyjson/bufferbuffer.Init()
String interning
string
string
internstringjson
type Foo struct {
UUID string `json:"uuid"` // will not be interned during unmarshaling
State string `json:"state,intern"` // will be interned during unmarshaling
}
问题、注意事项和限制
encoding/jsonencoding/jsonunsafe[]bytestringunsafeunsafeeasyjson_nounsafeunsafeappengineunsafestrconvpackage main
Benchmarks
大多数基准测试都是使用示例13kB示例JSON(去掉空白后为9k)完成的。这个例子类似于real-world数据,是well-structured,并且包含各种不同的类型,因此非常适合于JSON序列化基准测试。
Note:
unsafe
make
easyjson与encoding/json
encoding/json
easyjson与ffjson
easyjson使用与ffjson相同的JSON封送处理方法,但在解组期间采用了截然不同的方法来对JSON进行词法分析和解析。这意味着easyjson的解组速度大约快2-3倍,而non-concurrent解组速度快1.5-2x。
ffjsonffjsonffjsonffjson
ffjsonffjson
easyjson与go/codec
go/codec为JSON生成提供compile-time助手。在本例中,helper的工作方式与encoding-independent不同。
go/codec
go/codec
easyjson与“ujson”python模块
ujson正在使用C代码进行解析,因此很有兴趣了解一下普通golang与之相比的情况。需要注意的是,python的结果对象访问速度较慢,因为库将JSON对象解析为字典。
ujson
Benchmark Results
ffjsonffjsongo/codecgo/codec
Unmarshaling
lib | json size | MB/s | allocs/op | B/op |
---|---|---|---|---|
standard | regular | 22 | 218 | 10229 |
standard | small | 9.7 | 14 | 720 |
easyjson | regular | 125 | 128 | 9794 |
easyjson | small | 67 | 3 | 128 |
ffjson | regular | 66 | 141 | 9985 |
ffjson | small | 17.6 | 10 | 488 |
codec | regular | 55 | 434 | 19299 |
codec | small | 29 | 7 | 336 |
ujson | regular | 103 | N/A | N/A |
编组,一个goroutine。
lib | json size | MB/s | allocs/op | B/op |
---|---|---|---|---|
standard | regular | 75 | 9 | 23256 |
standard | small | 32 | 3 | 328 |
standard | large | 80 | 17 | 1.2M |
easyjson | regular | 213 | 9 | 10260 |
easyjson* | regular | 263 | 8 | 742 |
easyjson | small | 125 | 1 | 128 |
easyjson | large | 212 | 33 | 490k |
easyjson* | large | 262 | 25 | 2879 |
ffjson | regular | 122 | 153 | 21340 |
ffjson** | regular | 146 | 152 | 4897 |
ffjson | small | 36 | 5 | 384 |
ffjson** | small | 64 | 4 | 128 |
ffjson | large | 134 | 7317 | 818k |
ffjson** | large | 125 | 7320 | 827k |
codec | regular | 80 | 17 | 33601 |
codec*** | regular | 108 | 9 | 1153 |
codec | small | 42 | 3 | 304 |
codec*** | small | 56 | 1 | 48 |
codec | large | 73 | 483 | 2.5M |
codec*** | large | 103 | 451 | 66007 |
ujson | regular | 92 | N/A | N/A |
ffjson.Pool()
Marshaling, concurrent.
lib | json size | MB/s | allocs/op | B/op |
---|---|---|---|---|
standard | regular | 252 | 9 | 23257 |
standard | small | 124 | 3 | 328 |
standard | large | 289 | 17 | 1.2M |
easyjson | regular | 792 | 9 | 10597 |
easyjson* | regular | 1748 | 8 | 779 |
easyjson | small | 333 | 1 | 128 |
easyjson | large | 718 | 36 | 548k |
easyjson* | large | 2134 | 25 | 4957 |
ffjson | regular | 301 | 153 | 21629 |
ffjson** | regular | 707 | 152 | 5148 |
ffjson | small | 62 | 5 | 384 |
ffjson** | small | 282 | 4 | 128 |
ffjson | large | 438 | 7330 | 1.0M |
ffjson** | large | 131 | 7319 | 820k |
codec | regular | 183 | 17 | 33603 |
codec*** | regular | 671 | 9 | 1157 |
codec | small | 147 | 3 | 304 |
codec*** | small | 299 | 1 | 48 |
codec | large | 190 | 483 | 2.5M |
codec*** | large | 752 | 451 | 77574 |
ffjson.Pool()