Golang高性能jsonパッケージ:easyjson
概要
easyjsonって何ですか?公式サイトによると、easyjsonは効率的で迅速で使いやすい構造体structsjson変換パッケージを提供している.easyjsonは反射方式で実現されていないため,他のjsonパケットの4−5倍,golangが持参したjsonパケットの2−3倍の性能を有している.easyjsonの目標は、生成脱コードを簡単に維持し、容易に最適化または固定することができるようにすることである.
インストール
go get -u github.com/mailru/easyjson/go install github.com/mailru/easyjson/easyjsonorgo build -o easyjson github.com/mailru/easyjson/easyjson
正常にインストールされていることを確認します.
$ easyjson
Usage of D:\Code\go\bin\easyjson.exe:
-all generate marshaler/unmarshalers for all structs in a file
-build_tags string build tags to add to generated file
-leave_temps do not delete temporary files
-lower_camel_case use lowerCamelCase names instead of CamelCase by default
-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
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
-stubs only generate stubs for marshaler/unmarshaler funcs
いくつかのオプションに注意してください.
-lower_camel_case: field 。 Name=>name。
-build_tags string: string go 。
-no_std_marshalers: MarshalJSON/UnmarshalJSON 。
-omit_empty: field json, field 。-output_filename: 。-pkg: `//easyjson:json` easyjson 。-snke_case: field `Name_Student` `name_student`。
使用
easyjson
を使用する必要がある構造体に//easyjson:json
を加えたことを覚えておいてください.次のようになります.//easyjson:jsontype School struct { Name string `json:"name"`
Addr string `json:"addr"`}//easyjson:jsontype Student struct {
Id int `json:"id"`
Name string `json:"s_name"`
School School `json:"s_chool"`
Birthday time.Time `json:"birthday"`}
構造体パッケージの下で実行
easyjson -all student.go
ディレクトリの下に新しいファイルが表示されます.
// Code generated by easyjson for marshaling/unmarshaling. DO NOT EDIT.package easyjsonimport (
json "encoding/json"
easyjson "github.com/mailru/easyjson"
jlexer "github.com/mailru/easyjson/jlexer"
jwriter "github.com/mailru/easyjson/jwriter")// suppress unused package warningvar (
_ *json.RawMessage
_ *jlexer.Lexer
_ *jwriter.Writer
_ easyjson.Marshaler)func easyjsonB83d7b77DecodeStudygoEasyjson(in *jlexer.Lexer, out *Student) {
isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { in.Consumed()
} in.Skip() return
} in.Delim('{') for !in.IsDelim('}') {
key := in.UnsafeString() in.WantColon() if in.IsNull() { in.Skip() in.WantComma() continue
} switch key { case "id": out.Id = int(in.Int()) case "s_name": out.Name = string(in.String()) case "s_chool":
easyjsonB83d7b77DecodeStudygoEasyjson1(in, &out.School) case "birthday": if data := in.Raw(); in.Ok() { in.AddError((out.Birthday).UnmarshalJSON(data))
} default: in.SkipRecursive()
} in.WantComma()
} in.Delim('}') if isTopLevel { in.Consumed()
}
}func easyjsonB83d7b77EncodeStudygoEasyjson(out *jwriter.Writer, in Student) { out.RawByte('{')
first := true
_ = first if !first { out.RawByte(',')
}
first = false
out.RawString("\"id\":") out.Int(int(in.Id)) if !first { out.RawByte(',')
}
first = false
out.RawString("\"s_name\":") out.String(string(in.Name)) if !first { out.RawByte(',')
}
first = false
out.RawString("\"s_chool\":")
easyjsonB83d7b77EncodeStudygoEasyjson1(out, in.School) if !first { out.RawByte(',')
}
first = false
out.RawString("\"birthday\":") out.Raw((in.Birthday).MarshalJSON()) out.RawByte('}')
}// MarshalJSON supports json.Marshaler interfacefunc (v Student) MarshalJSON() ([]byte, error) {
w := jwriter.Writer{}
easyjsonB83d7b77EncodeStudygoEasyjson(&w, v) return w.Buffer.BuildBytes(), w.Error
}// MarshalEasyJSON supports easyjson.Marshaler interfacefunc (v Student) MarshalEasyJSON(w *jwriter.Writer) {
easyjsonB83d7b77EncodeStudygoEasyjson(w, v)
}// UnmarshalJSON supports json.Unmarshaler interfacefunc (v *Student) UnmarshalJSON(data []byte) error {
r := jlexer.Lexer{Data: data}
easyjsonB83d7b77DecodeStudygoEasyjson(&r, v) return r.Error()
}// UnmarshalEasyJSON supports easyjson.Unmarshaler interfacefunc (v *Student) UnmarshalEasyJSON(l *jlexer.Lexer) {
easyjsonB83d7b77DecodeStudygoEasyjson(l, v)
}func easyjsonB83d7b77DecodeStudygoEasyjson1(in *jlexer.Lexer, out *School) {
isTopLevel := in.IsStart() if in.IsNull() { if isTopLevel { in.Consumed()
} in.Skip() return
} in.Delim('{') for !in.IsDelim('}') {
key := in.UnsafeString() in.WantColon() if in.IsNull() { in.Skip() in.WantComma() continue
} switch key { case "name": out.Name = string(in.String()) case "addr": out.Addr = string(in.String()) default: in.SkipRecursive()
} in.WantComma()
} in.Delim('}') if isTopLevel { in.Consumed()
}
}func easyjsonB83d7b77EncodeStudygoEasyjson1(out *jwriter.Writer, in School) { out.RawByte('{')
first := true
_ = first if !first { out.RawByte(',')
}
first = false
out.RawString("\"name\":") out.String(string(in.Name)) if !first { out.RawByte(',')
}
first = false
out.RawString("\"addr\":") out.String(string(in.Addr)) out.RawByte('}')
}
テストクラスを書くことができます.
package mainimport ( "studygo/easyjson"
"time"
"fmt")func main(){
s:=easyjson.Student{
Id: 11,
Name:"qq",
School:easyjson.School{
Name:"CUMT",
Addr:"xz",
},
Birthday:time.Now(),
}
bt,err:=s.MarshalJSON()
fmt.Println(string(bt),err)
json:=`{"id":11,"s_name":"qq","s_chool":{"name":"CUMT","addr":"xz"},"birthday":"2017-08-04T20:58:07.9894603+08:00"}`
ss:=easyjson.Student{}
ss.UnmarshalJSON([]byte(json))
fmt.Println(ss)
}
実行結果:
{"id":11,"s_name":"qq","s_chool":{"name":"CUMT","addr":"xz"},"birthday":"2017-08-04T20:58:07.9894603+08:00"}
{121 {CwwwwwwwUMT xzwwwww} 2017-08-04 20:52:03.4066002 +0800 CST}