Examples in this folder demonstrate how to use third-party packages for JSON decoding and encoding.
The github.com/tidwall/gjson
package allows an
easy access to JSON properties, without parsing the payload into a data structure,
and is therefore convenient when accessing only selected parts of the response.
var json = `{"foo":{"bar":"BAZ"}}`
fmt.Println(gjson.Get(json, "foo.bar"))
// => BAZ
The gjson.go
example displays data from the
Cluster Stats API.
go run gjson.go
The github.com/mailru/easyjson
package uses code generation to
provide fast encoding and decoding of struct types.
The easyjson.go
example uses custom types for representing simpleArticle
documents
and index and search responses, including error responses.
make clean setup
go run easyjson.go
Both mailru/easyjson
and tidwall/gjson
provide significant performance gains in different scenarios.
You can run the included benchmarks by executing the make bench
command; example output below.
BenchmarkSearchResults/json 30000 45679 ns/op 8456 B/op 60 allocs/op
BenchmarkSearchResults/easyjson 100000 15612 ns/op 7992 B/op 52 allocs/op
BenchmarkClusterStats/json_-_map 20000 96808 ns/op 31971 B/op 391 allocs/op
BenchmarkClusterStats/json_-_struct 30000 59046 ns/op 15048 B/op 17 allocs/op
BenchmarkClusterStats/gjson 1000000 2119 ns/op 264 B/op 3 allocs/op