yson

package module
v0.0.0-...-4ad4f84 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 22, 2017 License: MIT Imports: 2 Imported by: 0

README

yson CircleCI Go Report Card GoDoc

Zero-allocation, human-friendly JSON library for Go 🍰

Status: experimental (API is not stable yet)

Usage

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

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.

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).

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).

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).

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.

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

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func EachKey

func EachKey(json []byte, fn func(key []byte))

Iterates though each key in json. Does nothing if json is not an object.

Memory usage: O(1), Time usage: O(n)

Example
package main

import (
	"fmt"

	"github.com/sheerun/yson"
)

func main() {
	json := []byte(`{
	  "foo": "bar",
	  "fiz": "fuz",
	  "1": "2"
	}`)

	yson.EachKey(json, func(key []byte) {
		fmt.Printf("%s ", string(key))
	})
}
Output:

foo fiz 1

func EachPair

func EachPair(json []byte, fn func(key []byte, value []byte))

Iterates though each key and value in json. Does nothing if json is not an object.

Use it for iterating over huge input, as it uses GC better than yson.EachStringKeyAndValue(json)

Memory usage: O(1), Time usage: O(n)

Example
package main

import (
	"fmt"

	"github.com/sheerun/yson"
)

func main() {
	json := []byte(`{
	  "foo": "bar",
	  "fiz": "fuz",
	  "1": "2"
	}`)

	yson.EachPair(json, func(key []byte, value []byte) {
		fmt.Printf("%s:%s ", key, value)
	})
}
Output:

foo:bar fiz:fuz 1:2

func EachValue

func EachValue(json []byte, fn func(value []byte))

Iterates though each value in json. Does nothing if json is not an object.

Memory usage: O(1), Time usage: O(n)

Example
package main

import (
	"fmt"

	"github.com/sheerun/yson"
)

func main() {
	json := []byte(`{
	  "foo": "bar",
	  "fiz": "fuz",
	  "1": "2"
	}`)

	yson.EachValue(json, func(key []byte) {
		fmt.Printf("%s ", string(key))
	})
}
Output:

bar fuz 2

func Get

func Get(json []byte, keys ...string) []byte

Gets single element from json.

Warning: the behavior for fetching items by numeric key is for now undefined.

Memory usage: O(1), Time usage: O(n)

Example
package main

import (
	"fmt"

	"github.com/sheerun/yson"
)

func main() {
	json := []byte(`{
	  "foo": "bar",
	  "fiz": "fuz"
	}`)

	fmt.Printf("%s", yson.Get(json, "foo"))
}
Output:

bar

func Load

func Load(json []byte, value interface{}) (err error)

Parses json to go-lang structure or value

Memory usage: O(1), Time usage: O(1)

Example
package main

import (
	"fmt"

	"github.com/sheerun/yson"
)

func main() {
	json := []byte(`{ "foo": 12 }`)

	var i int = 0
	err := yson.Load(yson.Get(json, "foo"), &i)

	if err != nil {
		panic(err)
	}

	fmt.Printf("%d", i+1)
}
Output:

13

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL