pick stuff from json - FAST
- fast.
- simple.
- lightweight. just stdlib
"encoding/json"
and ~2.0 KB - incomplete json still work well.
- no need to define parent key for nested target.
- read json as stream.
- stop when found.
- and you can also set the limit.
- we just need a part of the json.
- in case you need to parse all the json, please use the
"encoding/json"
Unmarshal
orDecoder
JSON_EXAMPLE := `{
"benchmark": "benchmark text 1",
"menu": {
"header": "SVG Viewer",
"image": {
"src": "Images/Sun.png",
"name": "sun1",
"hOffset": 250,
"vOffset": 250,
"alignment": "center",
"hidden": true
}
},
"benchmark": "benchmark text 2",
}`
- pick string
benchmarks := PickString(strings.NewReader(JSON_EXAMPLE), "benchmark", 0)
// [benchmark text 1 benchmark text 2]
- pick string just the 1st one
benchmarks := PickString(strings.NewReader(JSON_EXAMPLE), "benchmark", 1)
// [benchmark text 1]
- pick bool
hidden := PickBool(strings.NewReader(JSON_EXAMPLE), "hidden", 0)
// [true]
- pick object
type Image struct {
Src string `json:"src"`
Name string `json:"name"`
HOffset int `json:"hOffset"`
VOffset int `json:"vOffset"`
Alignment string `json:"alignment"`
}
var image Image
err := PickObject(strings.NewReader(JSON_EXAMPLE), "image", &image)
// {Images/Sun.png sun1 250 250 center}