loosy

package module
v0.0.0-...-5acd73f Latest Latest
Warning

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

Go to latest
Published: Feb 14, 2024 License: MIT Imports: 9 Imported by: 0

README

go-loosy

a performantant, light wrapper around github.com/json-iterator/go to provide lazy json parsing and generic value handling, all without any reflection!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsJSONObject

func AsJSONObject(b []byte) map[string]*JSON

Useful conversion methods for byte slices to / from map[string]*JSON.

func FromJSONArray

func FromJSONArray(j []*JSON) []byte

Useful conversion methods for byte slices to / from []*JSON.

func FromJSONObject

func FromJSONObject(j map[string]*JSON) []byte

Useful conversion methods for byte slices to / from map[string]*JSON.

func Marshal

func Marshal(v any) ([]byte, error)

Marshal: see github.com/json-iterator/go.Marshal() (using the default configuration).

func MarshalWrite

func MarshalWrite(w io.Writer, v any) error

MarshalWrite is a streamed alternative to Marshal().

func Unmarshal

func Unmarshal(data []byte, v any) error

Unmarshal: see github.com/json-iterator/go.Unmarshal() (using the default configuration).

func UnmarshalRead

func UnmarshalRead(r io.Reader, v any) error

UnmarshalRead is a streamed alternative to Unmarshal().

Types

type Any

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

Any represents a generic json value.

func (*Any) Array

func (a *Any) Array() []*Any

Array returns the value of Any{} cast to an array, else nil.

func (*Any) Field

func (a *Any) Field(name string) *Any

Field gets the field in Any{} with name, else nil.

func (*Any) Float

func (a *Any) Float() float64

Float returns the value of Any{} cast to a float64, else 0.

func (*Any) Format

func (a *Any) Format(state fmt.State, verb rune)

Format: implements fmt.Formatter for Any{}.

func (*Any) Int

func (a *Any) Int() int64

Int returns the value of Any{} cast to an int64, else 0.

func (*Any) IsArray

func (a *Any) IsArray() bool

IsArray returns whether Any{} is an possible type.

func (*Any) IsEmpty

func (a *Any) IsEmpty() bool

IsEmpty ...

func (*Any) IsNull

func (a *Any) IsNull() bool

IsNull returns whether Any{} is the json "null" value.

func (*Any) IsObject

func (a *Any) IsObject() bool

IsObject returns whether Any{} is an object type.

func (*Any) IterElems

func (a *Any) IterElems(fn func(int, *Any))

IterElems iterates the elements of Any{} cast as an array, successfuly casted as-so, or handling as a single element.

func (*Any) IterFields

func (a *Any) IterFields(fn func(string, *Any))

IterFields iterates the fields of Any{} cast as an object.

func (*Any) MarshalJSON

func (a *Any) MarshalJSON() ([]byte, error)

MarshalJSON formats Any{} as json data.

func (*Any) Object

func (a *Any) Object() map[string]*Any

Object returns the value of Any{} cast to an object, else nil.

func (*Any) SetArray

func (a *Any) SetArray(arr []*Any)

SetArray sets the underlying value of Any{} to given array.

func (*Any) SetFloat

func (a *Any) SetFloat(f float64)

SetFloat sets the underlying value of Any{} to given float64.

func (*Any) SetInt

func (a *Any) SetInt(i int64)

SetInt sets the underlying value of Any{} to given int64.

func (*Any) SetNull

func (a *Any) SetNull()

SetNull sets the Any{} data to the json "null" value.

func (*Any) SetObject

func (a *Any) SetObject(obj map[string]*Any)

SetObject sets the underlying value of Any{} to given object.

func (*Any) SetString

func (a *Any) SetString(s string)

SetString sets the underlying value of Any{} to given string.

func (*Any) SetUint

func (a *Any) SetUint(u uint64)

SetUint sets the underlying value of Any{} to given uint64.

func (*Any) String

func (a *Any) String() string

String returns the value of Any{} cast to a string, else "".

func (*Any) Type

func (a *Any) Type() interface{}

Type returns the underlying Any{} interface value.

func (*Any) Uint

func (a *Any) Uint() uint64

Uint returns the value of Any{} cast to an uint64, else 0.

func (*Any) UnmarshalJSON

func (a *Any) UnmarshalJSON(data []byte) error

UnmarshalJSON parses the given json data into Any{}.

type JSON

type JSON struct{ B []byte }

JSON represents unparsed json data, the helper methods allow you to lazily parse the data *as* you need it.

func AsJSON

func AsJSON(b []byte) *JSON

Useful conversion method to wrap a byte slice in JSON{}.

func AsJSONArray

func AsJSONArray(b []byte) []*JSON

Useful conversion methods for byte slices to / from []*JSON.

func (*JSON) Any

func (j *JSON) Any() (a *Any, err error)

Any unmarshals the data in JSON{} as the Any{} type.

func (*JSON) Arr

func (j *JSON) Arr() []*JSON

Arr is an alternative to Array() that disregards any errors.

func (*JSON) Array

func (j *JSON) Array() (arr []*JSON, err error)

Array unmarshals the data in JSON{} as []*JSON{}.

func (*JSON) Bool

func (j *JSON) Bool() (b bool, err error)

Bool unmarshals the data in JSON{} as bool.

func (*JSON) Float

func (j *JSON) Float() (f float64, err error)

Float unmarshals the data in JSON{} as float64.

func (*JSON) Int

func (j *JSON) Int() (i int64, err error)

Int unmarshals the data in JSON{} as int64.

func (*JSON) IsArray

func (j *JSON) IsArray() bool

IsArray returns whether JSON{} is a possible array (i.e. has surrounding square braces).

func (*JSON) IsEmpty

func (j *JSON) IsEmpty() bool

IsEmpty ...

func (*JSON) IsNull

func (j *JSON) IsNull() bool

IsNull returns whether JSON{} data is the json "null" value.

func (*JSON) IsObject

func (j *JSON) IsObject() bool

IsObject returns whether JSON{} is a possible object (i.e. has surrounding curl braces).

func (*JSON) MarshalJSON

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

MarshalJSON directly returns the data stored in JSON{}, or the json "null" value if nil.

func (*JSON) Obj

func (j *JSON) Obj() map[string]*JSON

Obj is an alternative to Object() that disregards any errors.

func (*JSON) Object

func (j *JSON) Object() (obj map[string]*JSON, err error)

Object unmarshals the data in JSON{} as object.

func (*JSON) SetAny

func (j *JSON) SetAny(a *Any)

SetAny sets the data in JSON{} to marshaled Any{} bytes.

func (*JSON) SetArray

func (j *JSON) SetArray(arr []*JSON)

SetArray sets the data in JSON{} to marshaled []*JSON{} bytes.

func (*JSON) SetBool

func (j *JSON) SetBool(b bool)

SetBool sets the data in JSON{} to marshaled bool bytes.

func (*JSON) SetFloat

func (j *JSON) SetFloat(f float64)

SetFloat sets the data in JSON{} to marshaled float64 bytes.

func (*JSON) SetFloatLossy

func (j *JSON) SetFloatLossy(f float64)

SetFloatLossy sets the data in JSON{} to marshaled float64 bytes (much faster, though with only 6 digits precision).

func (*JSON) SetInt

func (j *JSON) SetInt(i int64)

SetInt sets the data in JSON{} to marshaled int64 bytes.

func (*JSON) SetNull

func (j *JSON) SetNull()

SetNull sets the JSON{} data to the json "null" value.

func (*JSON) SetObject

func (j *JSON) SetObject(obj map[string]*JSON)

SetObject sets the data in JSON{} to marshaled object bytes.

func (*JSON) SetString

func (j *JSON) SetString(s string)

SetString sets the data in JSON{} to marshaled string bytes.

func (*JSON) SetStringHTMLEscaped

func (j *JSON) SetStringHTMLEscaped(s string)

SetString sets the data in JSON{} to marshaled string bytes (with HTML special chars escaped).

func (*JSON) SetUint

func (j *JSON) SetUint(u uint64)

SetUint sets the data in JSON{} to marshaled uint64 bytes.

func (*JSON) SetValue

func (j *JSON) SetValue(v any) (err error)

SetValue sets the data in JSON{} to marshaled value 'v' bytes.

func (*JSON) String

func (j *JSON) String() (s string, err error)

String unmarshals the data in JSON{} as string.

func (*JSON) Uint

func (j *JSON) Uint() (u uint64, err error)

Uint unmarshals the data in JSON{} as uint64.

func (*JSON) UnmarshalJSON

func (j *JSON) UnmarshalJSON(data []byte) error

UnmarshalJSON copies the data into JSON{}, delaying actual unmarshaling.

func (*JSON) Value

func (j *JSON) Value(v any) (err error)

Value unmarshals the data in JSON{} into value 'v'.

func (*JSON) Write

func (j *JSON) Write(p []byte) (n int, err error)

Write: implements io.Writer for JSON{}.

Jump to

Keyboard shortcuts

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