simplejson

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2021 License: MIT, MIT Imports: 9 Imported by: 0

README

go-simplejson

a Go package to interact with arbitrary JSON

Build Status

Importing

import github.com/bitly/go-simplejson

Documentation

Visit the docs on gopkgdoc

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Version

func Version() string

returns the current implementation version

Types

type Json

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

func New

func New() *Json

New returns a pointer to a new, empty `Json` object type map

func NewArray

func NewArray(c int) *Json

NewArray returns a pointer to a new, empty `Json` object type slice

func NewFromReader

func NewFromReader(r io.Reader) (*Json, error)

NewFromReader returns a *Json by decoding from an io.Reader

func NewJson

func NewJson(body []byte) (*Json, error)

NewJson returns a pointer to a new `Json` object after unmarshaling `body` bytes

func Wrap

func Wrap(val interface{}) *Json

Wrap returns a pointer to a new, `Json` object

func (*Json) AddArray

func (j *Json) AddArray(val interface{})

AddArray adds val to the `Json` slice Useful for adding value in a `Json` slice easily.

func (*Json) Array

func (j *Json) Array() ([]interface{}, error)

Array type asserts to an `array`

func (*Json) ArrayLen

func (j *Json) ArrayLen() int

ArrayLen returns a length current slice

func (*Json) Bool

func (j *Json) Bool() (bool, error)

Bool type asserts to `bool`

func (*Json) Bytes

func (j *Json) Bytes() ([]byte, error)

Bytes type asserts to `[]byte`

func (*Json) CheckGet

func (j *Json) CheckGet(key string) (*Json, bool)

CheckGet returns a pointer to a new `Json` object and a `bool` identifying success or failure

useful for chained operations when success is important:

if data, ok := js.Get("top_level").CheckGet("inner"); ok {
    log.Println(data)
}

func (*Json) Del

func (j *Json) Del(key string)

Del modifies `Json` map by deleting `key` if it is present.

func (*Json) Encode

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

Encode returns its marshaled data as `[]byte`

func (*Json) EncodePretty

func (j *Json) EncodePretty() ([]byte, error)

EncodePretty returns its marshaled data as `[]byte` with indentation

func (*Json) Float64

func (j *Json) Float64() (float64, error)

Float64 coerces into a float64

func (*Json) Get

func (j *Json) Get(key string) *Json

Get returns a pointer to a new `Json` object for `key` in its `map` representation

useful for chaining operations (to traverse a nested JSON):

js.Get("top_level").Get("dict").Get("value").Int()

func (*Json) GetIndex

func (j *Json) GetIndex(index int) *Json

GetIndex returns a pointer to a new `Json` object for `index` in its `array` representation

this is the analog to Get when accessing elements of a json array instead of a json object:

js.Get("top_level").Get("array").GetIndex(1).Get("key").Int()

func (*Json) GetPath

func (j *Json) GetPath(branch ...string) *Json

GetPath searches for the item as specified by the branch without the need to deep dive using Get()'s.

js.GetPath("top_level", "dict")

func (*Json) GetPath2

func (j *Json) GetPath2(branches string) *Json

GetPath2 does for a single joint branch name like: js.GetPath2("top_level.dict") It does the same thing like js.GetPath("top_level", "dict")

func (*Json) Int

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

Int coerces into an int

func (*Json) Int64

func (j *Json) Int64() (int64, error)

Int64 coerces into an int64

func (*Json) Interface

func (j *Json) Interface() interface{}

Interface returns the underlying data

func (*Json) Map

func (j *Json) Map() (map[string]interface{}, error)

Map type asserts to `map`

func (*Json) MarshalJSON

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

Implements the json.Marshaler interface.

func (*Json) MustArray

func (j *Json) MustArray(args ...[]interface{}) []interface{}

MustArray guarantees the return of a `[]interface{}` (with optional default)

useful when you want to interate over array values in a succinct manner:

for i, v := range js.Get("results").MustArray() {
	fmt.Println(i, v)
}

func (*Json) MustBool

func (j *Json) MustBool(args ...bool) bool

MustBool guarantees the return of a `bool` (with optional default)

useful when you explicitly want a `bool` in a single value return context:

myFunc(js.Get("param1").MustBool(), js.Get("optional_param").MustBool(true))

func (*Json) MustFloat64

func (j *Json) MustFloat64(args ...float64) float64

MustFloat64 guarantees the return of a `float64` (with optional default)

useful when you explicitly want a `float64` in a single value return context:

myFunc(js.Get("param1").MustFloat64(), js.Get("optional_param").MustFloat64(5.150))

func (*Json) MustInt

func (j *Json) MustInt(args ...int) int

MustInt guarantees the return of an `int` (with optional default)

useful when you explicitly want an `int` in a single value return context:

myFunc(js.Get("param1").MustInt(), js.Get("optional_param").MustInt(5150))

func (*Json) MustInt64

func (j *Json) MustInt64(args ...int64) int64

MustInt64 guarantees the return of an `int64` (with optional default)

useful when you explicitly want an `int64` in a single value return context:

myFunc(js.Get("param1").MustInt64(), js.Get("optional_param").MustInt64(5150))

func (*Json) MustMap

func (j *Json) MustMap(args ...map[string]interface{}) map[string]interface{}

MustMap guarantees the return of a `map[string]interface{}` (with optional default)

useful when you want to interate over map values in a succinct manner:

for k, v := range js.Get("dictionary").MustMap() {
	fmt.Println(k, v)
}

func (*Json) MustString

func (j *Json) MustString(args ...string) string

MustString guarantees the return of a `string` (with optional default)

useful when you explicitly want a `string` in a single value return context:

myFunc(js.Get("param1").MustString(), js.Get("optional_param").MustString("my_default"))

func (*Json) MustStringArray

func (j *Json) MustStringArray(args ...[]string) []string

MustStringArray guarantees the return of a `[]string` (with optional default)

useful when you want to interate over array values in a succinct manner:

for i, s := range js.Get("results").MustStringArray() {
	fmt.Println(i, s)
}

func (*Json) MustUint64

func (j *Json) MustUint64(args ...uint64) uint64

MustUInt64 guarantees the return of an `uint64` (with optional default)

useful when you explicitly want an `uint64` in a single value return context:

myFunc(js.Get("param1").MustUint64(), js.Get("optional_param").MustUint64(5150))

func (*Json) Set

func (j *Json) Set(key string, val interface{})

Set modifies `Json` map by `key` and `value` Useful for changing single key/value in a `Json` object easily.

func (*Json) SetPath

func (j *Json) SetPath(branch []string, val interface{})

SetPath modifies `Json`, recursively checking/creating map keys for the supplied path, and then finally writing in the value

func (*Json) String

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

String type asserts to `string`

func (*Json) StringArray

func (j *Json) StringArray() ([]string, error)

StringArray type asserts to an `array` of `string`

func (*Json) ToBool

func (j *Json) ToBool() bool

ToBool type cast to `bool`

func (*Json) ToInt

func (j *Json) ToInt() int

ToInt type cast to `int`

func (*Json) ToString

func (j *Json) ToString() string

ToString type cast to `string`

func (*Json) Uint64

func (j *Json) Uint64() (uint64, error)

Uint64 coerces into an uint64

func (*Json) UnmarshalJSON

func (j *Json) UnmarshalJSON(p []byte) error

Implements the json.Unmarshaler interface.

Jump to

Keyboard shortcuts

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