This is a created article in which the information may have evolved or changed.
Go comes with a encoding/json that supports serialization and deserialization of JSON, however it is based on reflection and has several drawbacks:
- Reflection is synonymous with poor performance and cannot be optimized at compile time.
- Only the public field is available, and the reflection library can only reflect the exposed fields, which means that you cannot json convert the private field.
Https://github.com/benbjohnson/megajson is optimized for this problem, which is a code generation tool that produces custom serial numbers and deserialization code for your own type entities by using the Go/parser and go/ast packages. The serial number and deserialization here know your data type, so there is no reflection and then the problem is solved.
With the test, Go 1.2, Megajson twice times better performance than the Encoding/json package.
Installation method:
$ go Get Github.com/benbjohnson/megajson
Use:
Run the Megajson command directly, which will produce the serialized and deserialized files you need:
$ Megajson Mypkg/my_file.go
This will result in 2 files:
mypkg/my_file_encoder.gomypkg/my_file_decoder.go
It is relatively simple to use, as follows:
ERR: = Newmystructencoder (writer). Encode (val) Err: = Newmystructdecoder (reader). Decode (&val)