json

package
v0.3.3 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2024 License: MIT, MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	JT_NULL = iota
	JT_TRUE
	JT_FALSE
	JT_NUMERIC
	JT_UTF8
	JT_ARRAY
	JT_OBJECT
	JT_INVALID
)
View Source
const (
	SS_NORMAL = iota
	SS_ESC
)

Variables

View Source
var (
	ErrIndexExceededMaximumLength = errors.New("index exceeded maximum length of j.data")
)

Functions

func Parse

func Parse(data []byte) (v interface{}, e error)

func Stringify

func Stringify(v interface{}) (buf []byte, e error)

Stringify support map []map. not support struct, because dont use reflect.

Types

type Decoder

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

func NewDecoder

func NewDecoder(store ObjectStore, data []byte) *Decoder

func (*Decoder) Decode

func (j *Decoder) Decode() (interface{}, error)

type JSON

type JSON struct {
	Error error
	// contains filtered or unexported fields
}

func New added in v0.2.0

func New(data []byte, opts ...Option) (j *JSON)

func (*JSON) Array

func (j *JSON) Array(args ...[]interface{}) []*JSON

Array guarantees the return of an `[]*JSON` (with optional default)

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

myFunc(js.Get("param1").Array(), js.Get("optional_param").Array([]interface{}{"string", 1, 1.1, false}))

func (*JSON) Bool

func (j *JSON) Bool(args ...bool) bool

Bool guarantees the return of an `bool` (with optional default)

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

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

func (*JSON) Float64

func (j *JSON) Float64(args ...float64) float64

Float64 guarantees the return of an `float64` (with optional default)

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

myFunc(js.Get("param1").Float64(), js.Get("optional_param").Float64(51.15))

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) Interface

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

func (*JSON) Map

func (j *JSON) Map(args ...map[string]interface{}) map[string]interface{}

Map 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").Map() {
	fmt.Println(k, v)
}

func (*JSON) MaybeArray

func (j *JSON) MaybeArray() ([]*JSON, error)

MaybeArray type asserts to `*[]interface{}`

func (*JSON) MaybeBool

func (j *JSON) MaybeBool() (bool, error)

MaybeBool type asserts and parses an `bool`

func (*JSON) MaybeFloat64

func (j *JSON) MaybeFloat64() (float64, error)

MaybeFloat64 type asserts and parses an `float64`

func (*JSON) MaybeMap

func (j *JSON) MaybeMap() (map[string]interface{}, error)

MaybeMap type asserts to `map`

func (*JSON) MaybeString

func (j *JSON) MaybeString() (string, error)

MaybeString type asserts to `string`

func (*JSON) String

func (j *JSON) String(args ...string) string

String 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").String(), js.Get("optional_param").String("my_default"))

type ObjectStore

type ObjectStore interface {
	NewObject() (interface{}, error)
	NewArray() (interface{}, error)
	ObjectAddKey(interface{}, interface{}, interface{}) error
	ArrayAddItem(interface{}, interface{}) error
	NewString([]byte) (interface{}, error)
	NewNumeric([]byte) (interface{}, error)
	NewTrue() (interface{}, error)
	NewFalse() (interface{}, error)
	NewNull() (interface{}, error)
}

type Option added in v0.2.0

type Option func(*cfg)

func WithStore added in v0.2.0

func WithStore(store ObjectStore) Option

Jump to

Keyboard shortcuts

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