Zero-allocation, human-friendly JSON library for Go ๐ฐ
Status: experimental (API is not stable yet)
Here's more advanced example:
import "github.com/sheerun/yson"
json := byte[](`{
"humans": {
"Adam": {
"happy": true,
"age": 9
},
"John": {
"happy": false,
"age": 12
}
}
}`)
yson.EachValue(yson.Get(json, "humans"), func(value []byte) {
fmt.Printf("%s ", yson.Get(value, "age"))
})
// Output: 9 12
Yson functions accept JSON is raw byte[]
form. Most of them don't allocate memory but just return slices of it.
Gets a value from JSON object. Can accept multiple keys. Returns nil
on any incorrect input.
json := byte[](`{
"Adam": { "age": 9 },
"John": { "age": 12 }
}`)
if age := yson.Get(json, "Adam", "age"); age != nil {
fmt.Printf("%s", age)
}
// Output: 9
Iterates over JSON keys. Does nothing on any incorrect input (including nil
).
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.EachKey(json, func(key []byte) {
fmt.Printf("%s ", key)
})
// Output: Adam John
Iterates over JSON values. Does nothing on any incorrect input (including nil
).
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.EachValue(json, func(value []byte) {
fmt.Printf("%s ", value)
})
// Output: 9 12
Iterates over JSON keys and values. Does nothing on any incorrect input (including nil
).
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.EachPair(json, func(key []byte, value []byte) {
fmt.Printf("%s=%s ", key, value)
})
// Output: Adam=9 John=12
Parses JSON value to go-lang structure or value.
var ages map[string]int
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.Load(json, ages)
fmt.Printf("%s %s", ages["Adam"], ages["John"])
// Output: 9 12
MIT