https://github.com/go-restit/lzjson
A flexible JSON decoding library in Golang
https://github.com/go-restit/lzjson
json-library json-structure
Last synced: 2 months ago
JSON representation
A flexible JSON decoding library in Golang
- Host: GitHub
- URL: https://github.com/go-restit/lzjson
- Owner: go-restit
- License: mit
- Created: 2016-02-06T18:20:07.000Z (over 10 years ago)
- Default Branch: master
- Last Pushed: 2016-12-06T09:56:02.000Z (over 9 years ago)
- Last Synced: 2026-01-14T13:39:32.488Z (5 months ago)
- Topics: json-library, json-structure
- Language: Go
- Size: 34.2 KB
- Stars: 32
- Watchers: 1
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# lzjson [![GoDoc][godoc-badge]][godoc] [![Travis CI results][travis-badge]][travis] [![AppVeyor][appveyor-badge]][appveyor] [![Coverage Status][coveralls-badge]][coveralls]
**lzjson** is a JSON decoding library aims to make you lazy.
Golang default [JSON library](https://golang.org/pkg/encoding/json/) requires to
provide certain data structure (e.g. struct) to decode data to. It is hard to
write type-inspecific code to examine JSON data structure. It is also hard to
determine the abcence or prescence of data field.
This library provide flexible interface for writing generic JSON parsing code.
Key features:
* zero knowledge parsing: can read and examine JSON structure without
pre-defining the data structure before hand.
* lazy parsing: allow you to parse only a specific node into golang data
structure.
* compatibility: totally compatible with the default json library
[godoc]: https://godoc.org/github.com/go-restit/lzjson
[godoc-badge]: https://godoc.org/github.com/go-restit/lzjson?status.svg
[travis]: https://travis-ci.org/go-restit/lzjson?branch=master
[travis-badge]: https://api.travis-ci.org/go-restit/lzjson.svg?branch=master
[appveyor]: https://ci.appveyor.com/project/yookoala/lzjson?branch=master
[appveyor-badge]: https://ci.appveyor.com/api/projects/status/github/go-restit/lzjson?branch=master&svg=true
[coveralls]: https://coveralls.io/github/go-restit/lzjson?branch=master
[coveralls-badge]: https://coveralls.io/repos/github/go-restit/lzjson/badge.svg?branch=master
## Example Use
### Decode a JSON
Decode is straight forward with any [io.Reader](https://golang.org/pkg/io/#Reader)
implementation (e.g.
[http.Request.Body](https://golang.org/pkg/net/http/#Request),
[http.Response.Body](https://golang.org/pkg/net/http/#Response),
[strings.Reader](https://golang.org/pkg/strings/#Reader)).
For example, in a [http.HandlerFunc](https://golang.org/pkg/net/http/#HandlerFunc):
```go
import (
"net/http"
"github.com/go-restit/lzjson"
)
func handler(w http.ResponseWriter, r *http.Request) {
json := lzjson.Decode(r.Body)
...
...
}
```
Or as a client:
```go
func main() {
resp, _ := http.Get("http://foobarapi.com/things")
json := lzjson.Decode(resp.Body)
...
...
}
```
### Get a node in an object or an array
You may retrieve the JSON value of any node.
```go
// get "foo" in the json
foo := json.Get("foo")
// get the 10th item in foo
// (like ordinary array, 0 is the first)
item10 := foo.GetN(9)
```
### Every node knows what it is
```go
body := strings.NewReader(`
{
"string": "hello world",
"number": 3.14,
"bool": true,
"array": [1, 2, 3, 5],
"object": {"foo": "bar"}
}
`)
json := lzjson.Decode(body)
fmt.Printf("%s", json.Get("string").Type()) // output "TypeString"
fmt.Printf("%s", json.Get("number").Type()) // output "TypeNumber"
fmt.Printf("%s", json.Get("bool").Type()) // output "TypeBool"
fmt.Printf("%s", json.Get("array").Type()) // output "TypeArray"
fmt.Printf("%s", json.Get("object").Type()) // output "TypeObject"
```
### Evaluating values a JSON node
For basic value types (string, int, bool), you may evaluate them directly.
```go
code := json.Get("code").Int()
message := json.Get("message").String()
```
### Partial Unmarsaling
You may decode only a child-node in a JSON structure.
```go
type Item struct {
Name string `json:"name"`
Weight int `json:"weight"`
}
var item Item
item10 := foo.GetN(9)
item10.Unmarshal(&item)
log.Printf("item: name=%s, weight=%d", item.Name, item.Weight)
```
### Chaining
You may chain `Get` and `GetN` to get somthing deep within.
```go
helloIn10thBar := lzjson.Decode(r.Body).Get("foo").GetN(9).Get("hello")
```
### Looping Object or Array
Looping is straight forward with `Len` and `GetKeys`.
```go
var item Item
for i := 0; i