ratchet: github.com/dailyburn/ratchet/data Index | Examples | Files

package data

import "github.com/dailyburn/ratchet/data"

Package data holds custom types and functions for passing JSON between ratchet stages.

Index

Examples

Package Files

json.go

func ObjectsFromJSON Uses

func ObjectsFromJSON(d JSON) ([]map[string]interface{}, error)

ObjectsFromJSON is a helper for parsing JSON into a slice of generic maps/objects. The use-case is when a stage is expecting to receive either a JSON object or an array of JSON objects, and want to deal with it in a generic fashion.

Code:

d := []byte(`[{"One":1},
		      {"Two":2}]`)

objects, _ := data.ObjectsFromJSON(d)

fmt.Println(fmt.Sprintf("%+v", objects))

Output:

[map[One:1] map[Two:2]]

func ParseJSON Uses

func ParseJSON(d JSON, v interface{}) error

ParseJSON is a simple wrapper for json.Unmarshal

Code:

d := []byte(`{"A":1,"B":2}`)
t := testStruct{}
data.ParseJSON(d, &t)

fmt.Println(fmt.Sprintf("%+v", t))

Output:

{A:1 B:2}

func ParseJSONSilent Uses

func ParseJSONSilent(d JSON, v interface{}) error

ParseJSONSilent won't log output when unmarshaling fails. It can be used in cases where failure is expected.

type JSON Uses

type JSON []byte

JSON is the data type that is passed along all data channels. Under the covers, JSON is simply a []byte containing JSON data.

func JSONFromHeaderAndRows Uses

func JSONFromHeaderAndRows(header []string, rows [][]interface{}) (JSON, error)

JSONFromHeaderAndRows takes the given header and rows of values, and turns it into a JSON array of objects.

Code:

header := []string{"A", "B", "C"}
rows := [][]interface{}{
    []interface{}{1, 2, 3},
    []interface{}{4, 5, 6},
}

d, _ := data.JSONFromHeaderAndRows(header, rows)

fmt.Println(fmt.Sprintf("%+v", string(d)))

Output:

[{"A":1,"B":2,"C":3},{"A":4,"B":5,"C":6}]

func NewJSON Uses

func NewJSON(v interface{}) (JSON, error)

NewJSON is a simple wrapper for json.Marshal.

Code:

t := testStruct{A: 1, B: 2}

d, _ := data.NewJSON(t)

fmt.Println(string(d))

Output:

{"A":1,"B":2}

Package data imports 4 packages (graph) and is imported by 10 packages. Updated 2016-07-31. Refresh now. Tools for package owners.