json

package
v1.106.0 Latest Latest
Warning

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

Go to latest
Published: Feb 29, 2024 License: MPL-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package json provides manipulation of JSON data.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

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

Data provides conveniences for working with JSON data.

func GetRequest

func GetRequest(url string) (statusCode int, body *Data, err error)

GetRequest calls http.Get with the URL and returns the response body as a new Data object.

func GetRequestWithContext added in v1.52.0

func GetRequestWithContext(ctx context.Context, url string) (statusCode int, body *Data, err error)

GetRequestWithContext calls http.Get with the URL and returns the response body as a new Data object.

func MustParse

func MustParse(data []byte) *Data

MustParse is the same as calling Parse, but without the error code on return.

func MustParseStream

func MustParseStream(in io.Reader) *Data

MustParseStream is the same as calling ParseStream, but without the error code on return.

func Parse

func Parse(data []byte) (*Data, error)

Parse JSON data from bytes. If the data can't be loaded, a valid, empty Data will still be returned, along with an error.

func ParseStream

func ParseStream(in io.Reader) (*Data, error)

ParseStream parses JSON data from the stream. If the data can't be loaded, a valid, empty Data will still be returned, along with an error.

func (*Data) Append

func (j *Data) Append(path string, value *Data) bool

Append a Data value to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendArray

func (j *Data) AppendArray(path string) bool

AppendArray appends a new array to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendBool

func (j *Data) AppendBool(path string, value bool) bool

AppendBool appends a bool to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendFloat64

func (j *Data) AppendFloat64(path string, value float64) bool

AppendFloat64 appends a float64 to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendInt64

func (j *Data) AppendInt64(path string, value int64) bool

AppendInt64 appends an int64 to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendMap

func (j *Data) AppendMap(path string) bool

AppendMap appends a new map to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) AppendStr

func (j *Data) AppendStr(path, value string) bool

AppendStr appends a string to an array at the specified path. The array must already exist. Returns true if successful.

func (*Data) Bool

func (j *Data) Bool(path string) bool

Bool extracts a bool from the path. Returns false if the path isn't present or isn't a boolean type.

func (*Data) BoolRelaxed

func (j *Data) BoolRelaxed(path string) bool

BoolRelaxed extracts a bool from the path. Returns false if the path isn't present or can't be converted to a boolean type.

func (*Data) Bytes

func (j *Data) Bytes() []byte

Bytes converts the data into a Data []byte.

func (*Data) Delete

func (j *Data) Delete(path string) bool

Delete a value at the specified path. Returns true if successful.

func (*Data) Exists

func (j *Data) Exists(path string) bool

Exists returns true if the path exists in the data.

func (*Data) Float64

func (j *Data) Float64(path string) float64

Float64 extracts an float64 from the path. Returns 0 if the path isn't present or isn't a numeric type.

func (*Data) Float64Relaxed

func (j *Data) Float64Relaxed(path string) float64

Float64Relaxed extracts an float64 from the path. Returns 0 if the path isn't present or can't be converted to a numeric type.

func (*Data) Index

func (j *Data) Index(index int) *Data

Index returns the object at the specified index within an array, or nil if this isn't an array or the index isn't valid.

func (*Data) Int64

func (j *Data) Int64(path string) int64

Int64 extracts an int64 from the path. Returns 0 if the path isn't present or isn't a numeric type.

func (*Data) Int64Relaxed

func (j *Data) Int64Relaxed(path string) int64

Int64Relaxed extracts an int64 from the path. Returns 0 if the path isn't present or can't be converted to a numeric type.

func (*Data) IsArray

func (j *Data) IsArray() bool

IsArray returns true if this is a Data array.

func (*Data) IsMap

func (j *Data) IsMap() bool

IsMap returns true if this is a Data map.

func (*Data) Keys

func (j *Data) Keys() []string

Keys returns the keys of a map, or an empty slice if this is not a map.

func (*Data) NewArray

func (j *Data) NewArray(path string) bool

NewArray creates an array at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) NewMap

func (j *Data) NewMap(path string) bool

NewMap creates a map at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) Path

func (j *Data) Path(path string) *Data

Path searches the dot-separated path and returns the object at that point. If the search encounters an array and has not reached the end target, then it will iterate through the array for the target and return all results in a Data array.

func (*Data) PostRequest

func (j *Data) PostRequest(url string) (statusCode int, body *Data, err error)

PostRequest calls http.Post with the URL and the contents of this Data object and returns the response body as a new Data object.

func (*Data) PostRequestWithContext added in v1.52.0

func (j *Data) PostRequestWithContext(ctx context.Context, url string) (statusCode int, body *Data, err error)

PostRequestWithContext calls http.Post with the URL and the contents of this Data object and returns the response body as a new Data object.

func (*Data) Raw

func (j *Data) Raw() any

Raw returns the underlying data.

func (*Data) Set

func (j *Data) Set(path string, value *Data) bool

Set a Data value at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) SetBool

func (j *Data) SetBool(path string, value bool) bool

SetBool a bool at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) SetFloat64

func (j *Data) SetFloat64(path string, value float64) bool

SetFloat64 a float64 at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) SetInt64

func (j *Data) SetInt64(path string, value int64) bool

SetInt64 an int64 at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) SetStr

func (j *Data) SetStr(path, value string) bool

SetStr a string at the specified path. Any parts of the path that do not exist will be created. Returns true if successful, or false if a collision occurs with a non-object type while traversing the path.

func (*Data) Size

func (j *Data) Size() int

Size returns the number of elements in an array or map, or 0 if this is neither type.

func (*Data) Str

func (j *Data) Str(path string) string

Str extracts a string from the path. Returns the empty string if the path isn't present or isn't a string type.

func (*Data) String

func (j *Data) String() string

String converts the data into a Data string.

func (*Data) Unmarshal

func (j *Data) Unmarshal(path string, value any) error

Unmarshal parses the data at the path and stores the result into value.

Jump to

Keyboard shortcuts

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