Giter VIP home page Giter VIP logo

yson's Introduction

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

yson's People

Contributors

sheerun avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sireof

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.