pickjson

package module
v0.0.0-...-c8760e0 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2017 License: MIT Imports: 2 Imported by: 7

README

pick-json Build Status Doc

pick stuff from json - FAST

Feature

  • fast.
  • simple.
  • lightweight. just stdlib "encoding/json" and ~2.0 KB
  • incomplete json still work well.
  • no need to define parent key for nested target.

Fast? How?

  • read json as stream.
  • stop when found.
  • and you can also set the limit.

Use cases

  • we just need a part of the json.
  • in case you need to parse all the json, please use the "encoding/json" Unmarshal or Decoder

Examples

JSON_EXAMPLE := `{   
    "benchmark": "benchmark text 1",
    "menu": {
        "header": "SVG Viewer",
        "image": { 
            "src": "Images/Sun.png",
            "name": "sun1",
            "hOffset": 250,
            "vOffset": 250,
            "alignment": "center",
            "hidden": true
        }
    },
    "benchmark": "benchmark text 2",
}`
  • pick string
benchmarks := PickString(strings.NewReader(JSON_EXAMPLE), "benchmark", 0)
// [benchmark text 1 benchmark text 2]
  • pick string just the 1st one
benchmarks := PickString(strings.NewReader(JSON_EXAMPLE), "benchmark", 1)
// [benchmark text 1]
  • pick bool
hidden := PickBool(strings.NewReader(JSON_EXAMPLE), "hidden", 0)
// [true]
  • pick object
type Image struct {
    Src       string `json:"src"`
    Name      string `json:"name"`
    HOffset   int    `json:"hOffset"`
    VOffset   int    `json:"vOffset"`
    Alignment string `json:"alignment"`
}

var image Image

err := PickObject(strings.NewReader(JSON_EXAMPLE), "image", &image)
// {Images/Sun.png sun1 250 250 center}

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func PickBool

func PickBool(reader io.Reader, key string, limit int) (res []bool)

PickBool pick Bool by key, return empty array if there is no matching key or error

Example

pick bool

hidden := PickBool(strings.NewReader(jsonExample), "hidden", 0)
fmt.Println(hidden)
Output:

[true]

func PickNumber

func PickNumber(reader io.Reader, key string, limit int) (res []float64)

PickNumber pick Float64 by key, return empty array if there is no matching key or error

func PickObject

func PickObject(reader io.Reader, key string, object interface{}) (err error)

PickObject pick struct by key there is no limit for PickObject yet :( TODO: add limit

Example

pick object

var img image

PickObject(strings.NewReader(jsonExample), "image", &img)
fmt.Println(img)
Output:

{Images/Sun.png sun1 250 250 center}

func PickString

func PickString(reader io.Reader, key string, limit int) (res []string)

PickString pick String by key, return empty array if there is no matching key or error

Example

pick string

benchmarks := PickString(strings.NewReader(jsonExample), "benchmark", 0)
fmt.Println(benchmarks)
Output:

[benchmark text 1 benchmark text 2]

Types

This section is empty.

Jump to

Keyboard shortcuts

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