jsonvalue

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2022 License: MIT Imports: 3 Imported by: 0

README

JSON Value

This package provides a Value type for getting values out of arbitrary json structure

val := jsonvalue.Parse([]byte(`{ "foo": { "bar": [123, 324, 1] } }`))
num, _ := val.Lookup("foo", "bar").Index(1).Num()
fmt.Println(num)

Documentation

Index

Constants

View Source
const (
	TypeObject = Type(iota)
	TypeArray
	TypeNum
	TypeStr
	TypeBool
	TypeNull
	TypeInvalid
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Type

type Type int

Type is the type of a value

func (Type) String

func (t Type) String() string

String implements fmt.Stringer

type Value

type Value struct {
	Err   error
	Path  []string
	Value interface{}
}

Value is an object, array, string, number, or bool

func Parse

func Parse(data []byte) Value

Parse json data into a Value

func (Value) Array

func (v Value) Array() ([]Value, error)

Array returns the value as a slice of values or an error if the value is not an array

func (Value) Bool

func (v Value) Bool() (bool, error)

Bool returns the value as a bool or an error if the value is not a bool

func (Value) Index

func (v Value) Index(i int) Value

Index returns the value at the specified index. If the value is not an array, the returned value will contain an error

func (Value) IsNull

func (v Value) IsNull() (bool, error)

IsNull returns true when the value is null

func (Value) Key

func (v Value) Key(key string) Value

Key returns the value at the specified key. If the value is not an object, the returned value will contain an error

func (Value) Len

func (v Value) Len() (int, error)

Len returns the array or string lenth or an error if the value is not an array

func (Value) Lookup

func (v Value) Lookup(keys ...string) Value

Lookup is a convenience method for calling Key multiple times

func (Value) Num

func (v Value) Num() (float64, error)

Num returns the value as a float64 or a n error if the value is not a float64

func (Value) Object

func (v Value) Object() (map[string]Value, error)

Object returns the value as a map of values or an error if the value is not an object

func (Value) Str

func (v Value) Str() (string, error)

Str returns the value as a string or an error if the value is not a string

func (Value) String

func (v Value) String() string

String implements fmt.Stringer

func (Value) Type

func (v Value) Type() Type

Type returns the value's type

func (Value) Walk

func (v Value) Walk(fn func(v Value) bool)

Walk calls fn with all elements of value recursively. When fn returns false, it doesn't recurse further.

Jump to

Keyboard shortcuts

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