https://github.com/sheerun/yson
Zero-allocation, human-friendly JSON library for Go
https://github.com/sheerun/yson
go-lang json utilities
Last synced: 11 months ago
JSON representation
Zero-allocation, human-friendly JSON library for Go
- Host: GitHub
- URL: https://github.com/sheerun/yson
- Owner: sheerun
- License: mit
- Created: 2017-07-04T01:00:49.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2017-08-22T01:37:11.000Z (almost 9 years ago)
- Last Synced: 2025-04-04T09:44:46.240Z (about 1 year ago)
- Topics: go-lang, json, utilities
- Language: Go
- Homepage:
- Size: 18.6 KB
- Stars: 4
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# yson [](https://circleci.com/gh/sheerun/yson) [](https://goreportcard.com/report/github.com/sheerun/yson) [](http://godoc.org/github.com/sheerun/yson)
Zero-allocation, human-friendly JSON library for Go :cake:
Status: experimental (API is not stable yet)
## Usage
Here's more advanced example:
```go
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
```
## API
Yson functions accept JSON is raw `byte[]` form. Most of them don't allocate memory but just return slices of it.
### yson.Get
Gets a value from JSON object. Can accept multiple keys. Returns `nil` on any incorrect input.
```go
json := byte[](`{
"Adam": { "age": 9 },
"John": { "age": 12 }
}`)
if age := yson.Get(json, "Adam", "age"); age != nil {
fmt.Printf("%s", age)
}
// Output: 9
```
### yson.EachKey
Iterates over JSON keys. Does nothing on any incorrect input (including `nil`).
```go
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.EachKey(json, func(key []byte) {
fmt.Printf("%s ", key)
})
// Output: Adam John
```
### yson.EachValue
Iterates over JSON values. Does nothing on any incorrect input (including `nil`).
```go
json := byte[](`{ "Adam": 9, "John": 12 }`)
yson.EachValue(json, func(value []byte) {
fmt.Printf("%s ", value)
})
// Output: 9 12
```
### yson.EachPair
Iterates over JSON keys and values. Does nothing on any incorrect input (including `nil`).
```go
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
```
### yson.Load
Parses JSON value to go-lang structure or value.
```go
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
```
## License
MIT