jsonhelper

package module
v0.0.0-...-56a14dd Latest Latest
Warning

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

Go to latest
Published: May 11, 2016 License: MIT Imports: 11 Imported by: 2

README

Go JSON Utilities GoDoc

JsonHelper

A Go Json Helper, focused on Type coercion, and json path query. You can use it for your request body, or pass json config to you function.

*This lib is forked from araddon/gou, and remove other utilities. json helper is remained.

Create a JsonHelper from json string

    jh := NewJsonHelper([]byte(`{
        "name":"aaron",
        "nullstring":null,
        "ints":[1,2,3,4],
        "int":1,
        "bool": false,
        "intstr":"1",
        "int64":1234567890,
        "MaxSize" : 1048576,
        "strings":["string1"],
        "stringscsv":"string1,string2",
        "nested":{
            "nest":"string2",
            "strings":["string1"],
            "int":2,
            "list":["value"],
            "nest2":{
                "test":"good"
            }
        },
        "nested2":[
            {"sub":2}
        ],
        "period.name":"value"
    }`)

Get Values

    // String method
    jh.String("name", "defaultValue")    // "aaron"
    jh.String("noname", "defaultValue")  // "defaultValue"
    
    
    // Int Method
    jh.Int("int", 0)    //1
    jh.Int("noint", 20) //20
    
    // Bool Method
    jh.Bool("bool", true)   // fale
    jh.Bool("nobool", true) // true

Get Value by Path

    jh.Int("nested.int", 0)             // 2
    jh.String("nested.nest", "")        // "string2"
    jh.String("nested.nest2.test", "")  // "good"
    jh.String("nested.list[0]", "")     // "value"
    jh.Int("nested2[0].sub", 10)        // 2

License

MIT License

Documentation

Index

Constants

View Source
const (
	MaxInt  = 1<<(BitsPerWord-1) - 1 // either 1<<31 - 1 or 1<<63 - 1
	MinInt  = -MaxInt - 1            // either -1 << 31 or -1 << 63
	MaxUint = 1<<BitsPerWord - 1     // either 1<<32 - 1 or 1<<64 - 1
)

Implementation-specific integer limit values.

View Source
const BitsPerWord = bitsPerWord // either 32 or 64

Implementation-specific size of int and uint in bits.

Variables

This section is empty.

Functions

func CoerceFloat

func CoerceFloat(v interface{}) (float64, error)

func CoerceFloatShort

func CoerceFloatShort(v interface{}) float64

func CoerceInt

func CoerceInt(v interface{}) (int, error)

func CoerceInt64

func CoerceInt64(v interface{}) (int64, error)

func CoerceInt64Short

func CoerceInt64Short(v interface{}) int64

func CoerceIntShort

func CoerceIntShort(v interface{}) int

func CoerceString

func CoerceString(v interface{}) (string, error)

Coerce types (string,int,int64, float, []byte) into String type

func CoerceStringShort

func CoerceStringShort(v interface{}) string

Coerce type to string, returning zero length string if error or nil

func CoerceUint

func CoerceUint(v interface{}) (uint64, error)

Coerce a val(interface{}) into a Uint64

func CoerceUintShort

func CoerceUintShort(v interface{}) uint64

Coerce a Val(interface{}) into Uint64

func IsJson

func IsJson(by []byte) bool

Determines if the bytes is a json array, only looks at prefix

not parsing the entire thing

func IsJsonArray

func IsJsonArray(by []byte) bool

Determines if the bytes is a json array, only looks at prefix

not parsing the entire thing

func IsJsonObject

func IsJsonObject(by []byte) bool

func JsonString

func JsonString(v interface{}) string

func MakeJsonList

func MakeJsonList(b []byte) []byte

Convert a slice of bytes into an array by ensuring it is wrapped

with []

Types

type JsonHelper

type JsonHelper map[string]interface{}

A wrapper around a map[string]interface{} to facilitate coercion of json data to what you want

allows usage such as this

jh := NewJsonHelper([]byte(`{
	"name":"string",
	"ints":[1,5,9,11],
	"int":1,
	"int64":1234567890,
	"MaxSize" : 1048576,
	"strings":["string1"],
	"nested":{
		"nest":"string2",
		"strings":["string1"],
		"int":2,
		"list":["value"],
		"nest2":{
			"test":"good"
		}
	},
	"nested2":[
		{"sub":5}
	]
}`)

i := jh.Int("nested.int")  // 2
i2 := jh.Int("ints[1]")    // 5   array position 1 from [1,5,9,11]
s := jh.String("nested.nest")  // "string2"

func NewJsonHelper

func NewJsonHelper(b []byte) JsonHelper

func NewJsonHelperFromResp

func NewJsonHelperFromResp(resp *http.Response) (JsonHelper, error)

Make a JsonHelper from http response. This will automatically close the response body

func NewJsonHelperReader

func NewJsonHelperReader(r io.Reader) (jh JsonHelper, err error)

func NewJsonHelpers

func NewJsonHelpers(b []byte) []JsonHelper

func (JsonHelper) Bool

func (j JsonHelper) Bool(n string, defVal bool) bool

func (JsonHelper) Float64

func (j JsonHelper) Float64(n string, defVal float64) float64

func (JsonHelper) Get

func (j JsonHelper) Get(n string) interface{}

Get the key (or keypath) value as interface, mostly used internally through String, etc methods

jh.Get("name.subname")
jh.Get("name/subname")
jh.Get("name.arrayname[1]")
jh.Get("name.arrayname[]")

func (*JsonHelper) GobDecode

func (j *JsonHelper) GobDecode(data []byte) error

GobDecode overwrites the receiver, which must be a pointer, with the value represented by the byte slice, which was written by GobEncode, usually for the same concrete type. GobDecode([]byte) error

func (*JsonHelper) GobEncode

func (j *JsonHelper) GobEncode() ([]byte, error)

func (JsonHelper) HasKey

func (j JsonHelper) HasKey(name string) bool

func (JsonHelper) Helper

func (j JsonHelper) Helper(n string) JsonHelper

Get a Helper from a string path

func (JsonHelper) Helpers

func (j JsonHelper) Helpers(n string) []JsonHelper

Get list of Helpers at given name. Trys to coerce into proper Helper type

func (JsonHelper) Int

func (j JsonHelper) Int(n string, defVal int) int

func (JsonHelper) Int64

func (j JsonHelper) Int64(n string, defVal int64) int64

func (JsonHelper) Ints

func (j JsonHelper) Ints(n string, defVal []int) []int

func (JsonHelper) Keys

func (j JsonHelper) Keys() []string

func (JsonHelper) List

func (j JsonHelper) List(n string, defVal []interface{}) []interface{}

Gets slice of interface{}

func (JsonHelper) Map

func (j JsonHelper) Map(n string, defVal map[string]interface{}) map[string]interface{}

func (JsonHelper) PrettyJson

func (j JsonHelper) PrettyJson() []byte

func (JsonHelper) String

func (j JsonHelper) String(n string, defVal string) string

func (JsonHelper) Strings

func (j JsonHelper) Strings(n string, defVal []string) []string

func (JsonHelper) Uint64

func (j JsonHelper) Uint64(n string, defVal uint64) uint64

type JsonInterface

type JsonInterface struct {
	// contains filtered or unexported fields
}

A simple wrapper to help json data be consumed when not using Strongly typed structs.

func (*JsonInterface) Encode

func (j *JsonInterface) Encode() ([]byte, error)

Encode returns its marshaled data as `[]byte`

func (*JsonInterface) Float

func (j *JsonInterface) Float() (float32, error)

Coerce to Float, return err if needed

func (JsonInterface) FloatSh

func (j JsonInterface) FloatSh() float32

Coerce to Float, 0 returned if 0 or missing

func (*JsonInterface) Int

func (j *JsonInterface) Int() (int, error)

Coerce to Int

func (JsonInterface) IntSh

func (j JsonInterface) IntSh() int

Coerce to Int, 0 returned if missing or zero

func (*JsonInterface) MarshalJSON

func (j *JsonInterface) MarshalJSON() ([]byte, error)

Implements the json.Marshaler interface.

func (*JsonInterface) String

func (j *JsonInterface) String() (string, error)

Coerce to a String

func (JsonInterface) StringSh

func (j JsonInterface) StringSh() string

Coerce to a string, may be zero length if missing, or zero length

func (*JsonInterface) UnmarshalJSON

func (j *JsonInterface) UnmarshalJSON(raw []byte) error

Implements the json.Unmarshal interface.

type JsonRawWriter

type JsonRawWriter struct {
	bytes.Buffer
}

func (*JsonRawWriter) MarshalJSON

func (m *JsonRawWriter) MarshalJSON() ([]byte, error)

func (*JsonRawWriter) Raw

func (m *JsonRawWriter) Raw() json.RawMessage

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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