json

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2024 License: MIT Imports: 15 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Config = jsoniter.Config{
	EscapeHTML:             false,
	SortMapKeys:            false,
	ValidateJsonRawMessage: false,
	UseNumber:              true,
}.Froze()

Config is a custom JSON configuration.

View Source
var ConfigSorted = jsoniter.Config{
	EscapeHTML:             false,
	SortMapKeys:            true,
	ValidateJsonRawMessage: false,
	UseNumber:              true,
}.Froze()

ConfigSorted is a custom JSON configuration.

View Source
var Empty = RawMessage("{}")

Empty is empty document

Functions

func AsArray

func AsArray(data []byte) ([]byte, error)

AsArray is a helper function to ensure a JSON document is an array.

func Coalesce

func Coalesce(getter func(Map) (bool, error), docs ...Map) (bool, error)

Coalesce is a helper function to coalesce a string from multiple documents.

func CoalesceString

func CoalesceString(ctx context.Context, path string, defVal string, docs ...Map) (string, error)

CoalesceString is a helper function to coalesce a string from multiple documents.

func Ensure

func Ensure(doc []byte) []byte

Ensure is a helper function to ensure a JSON document is not empty. It takes a document and returns an empty object if the document is empty.

func EnumProperties

func EnumProperties(
	object interface{},
) (list []string, err error)

EnumProperties is a helper function to export properties from an object. It takes an object and returns a map of properties. Example: ExportProperties(ctx, object)

func Get

func Get(
	doc []byte,
	getters ...func(Map) error,
) (err error)

Get is a helper function to extract a value from a JSON document. Example: Get(doc, GetInteger("key", 0))

func GetProperty

func GetProperty(
	ctx context.Context,
	object interface{},
	key string,
) (interface{}, error)

GetProperty is a helper function to extract a value from an object. It takes an object and a key and returns the value. It can panic. Example: GetProperty(ctx, object, "key")

func GetPropertyEx

func GetPropertyEx(
	ctx context.Context,
	object interface{},
	key string,
) (val interface{}, err error)

GetPropertyEx is a helper function to extract a value from an object It is wrapper for JsonGetProperty with panic handling. Example: GetPropertyEx(ctx, object, "key")

func ImportProperties

func ImportProperties(
	ctx context.Context,
	object interface{},
	getter types.Getter,
) error

ImportProperties is a helper function to import properties into an object. It takes an object and a getter and sets the properties. Example: ImportProperties(ctx, object, getter)

func IsArray

func IsArray(data []byte) (bool, error)

IsArray is a helper function to determine if a JSON document is an array.

func IsEmpty

func IsEmpty(doc []byte) bool

func IsEqual

func IsEqual(a, b RawMessage) bool

func IsObject

func IsObject(data []byte) (bool, error)

IsObject is a helper function to determine if a JSON document is an object.

func Marshal

func Marshal(value interface{}) ([]byte, error)

Marshal is a helper function to marshal a JSON document.

func MarshalIndent

func MarshalIndent(value interface{}) ([]byte, error)

MarshalIndent is a helper function to marshal a JSON document with indentation.

func Merge

func Merge(docs ...[]byte) ([]byte, error)

Merge is a helper function to merge JSON documents.

func RestoreString

func RestoreString(ctx context.Context, path string, docs ...Map) error

func SetProperty

func SetProperty(
	ctx context.Context,
	object interface{},
	key string,
	value interface{},
) error

SetProperty is a helper function to set a value in an object. It takes an object, a key and a value and sets the value. It can panic. Example: SetProperty(ctx, object, "key", "value")

func SetPropertyEx

func SetPropertyEx(
	ctx context.Context,
	object interface{},
	key string,
	val interface{},
) (err error)

SetPropertyEx is a helper function to set a value in an object. It is wrapper for JsonSetProperty with panic handling. Example: SetPropertyEx(ctx, object, "key", "value")

func TypeOf

func TypeOf(in io.Reader) (string, error)

TypeOf is a helper function to determine the type of a JSON document. It takes a document and returns the type (array, object). Example: TypeOf(doc)

func Unmarshal

func Unmarshal(raw []byte, value interface{}) error

Unmarshal is a helper function to unmarshal a JSON document.

func Update

func Update(
	doc []byte,
	actions ...func(Map) error,
) ([]byte, error)

Update is a helper function to update a JSON document. It takes a document and a function that takes a Map and returns an error. Example: Update(doc, func(m Map) error { m["key"] = "value"; return nil })

func UpdateAll

func UpdateAll(
	doc []byte,
	actions ...func(Map) error,
) ([]byte, error)

UpdateAll is a helper function to update a list of JSON documents.

func WithAny

func WithAny(key string, ref *any) func(Map) error

WihtAny is a helper function to extract a any value from a JSON document. See Get for more information.

func WithBoolean

func WithBoolean(key string, ref *bool) func(Map) error

WithBoolean is a helper function to extract a boolean from a JSON document. See Get for more information.

func WithDefaultValue

func WithDefaultValue(key string, val interface{}) func(Map) error

WithDefaultValue is a helper function to set a default value in a JSON document. See Update for more information.

func WithDuration

func WithDuration(key string, ref *time.Duration) func(Map) error

WithDuration is a helper function to extract a time from a JSON document. See Get for more information.

func WithFloat

func WithFloat(key string, ref *float64) func(Map) error

WithFloat is a helper function to extract a float from a JSON document. See Get for more information.

func WithIf

func WithIf(cond bool, action func(Map) error) func(Map) error

WithIf is a helper function to conditionally execute an action. See Update for more information.

func WithInteger

func WithInteger(key string, ref *int64) func(Map) error

WithInteger is a helper function to extract an integer from a JSON document. See Get for more information.

func WithJson

func WithJson(key string, ref *RawMessage) func(Map) error

WithJson is a helper function to extract a raw message from a JSON document. See Get for more information.

func WithRemove

func WithRemove(key string) func(Map) error

WithRemove is a helper function to remove a value from a JSON document.

func WithString

func WithString(key string, ref *string) func(Map) error

WithString is a helper function to extract a string from a JSON document. See Get for more information.

func WithValue

func WithValue(key string, val interface{}) func(Map) error

WithValue is a helper function to override a value in a JSON document. See Update for more information.

func WithValues

func WithValues(
	fields map[string]interface{},
) func(Map) error

WithValues is a helper function to update a JSON document with a map of values. It takes a document and a map of fields to update. Example: Update(doc, Values(map[string]interface{}{"key": "value"}))

Types

type AtomGetter

type AtomGetter interface {
	Get(ctx context.Context) (interface{}, error)
}

type AtomSetter

type AtomSetter interface {
	Set(ctx context.Context, value interface{}) error
}

type Boolean

type Boolean bool

Boolean is a custom type for JSON booleans.

func (*Boolean) MarshalJSON

func (b *Boolean) MarshalJSON() ([]byte, error)

func (*Boolean) UnmarshalJSON

func (b *Boolean) UnmarshalJSON(data []byte) error

type Logical

type Logical int

Logical is a custom type for JSON.

func (*Logical) MarshalJSON

func (b *Logical) MarshalJSON() ([]byte, error)

func (*Logical) UnmarshalJSON

func (b *Logical) UnmarshalJSON(data []byte) error

type Map

type Map map[string]interface{}

Map is standard map, that implements GetterSetter

func NewMap

func NewMap(doc []byte) (Map, error)

NewMap is constructor for creating Map from JSON source.

func NewMapFromFile

func NewMapFromFile(filename string) (Map, error)

NewMapFromFile returns map, that loaded from file

func NewMapFromFiles

func NewMapFromFiles(files ...string) (m Map, err error)

NewMapFromFiles returns map as result join from files.

func NewMapFromNativeStruct

func NewMapFromNativeStruct(val interface{}) Map

NewMapFromNativeStruct is constructor for creating Map from native struct.

func NewMapFromStruct

func NewMapFromStruct(
	m map[string]interface{},
) Map

NewMapFromStruct is constructor for creating Map from standard map.

func NewMaps

func NewMaps(raw []byte) ([]Map, error)

NewMaps is constructor for creating Maps from JSON source.

func (Map) Clone

func (that Map) Clone() Map

func (Map) Contains

func (that Map) Contains(name string) bool

func (Map) ExpandBy

func (that Map) ExpandBy(right Map)

ExpandBy is routine, that allow expand map by additional values from another map (recursive).

func (Map) GetBoolean

func (that Map) GetBoolean(
	ctx context.Context,
	name string,
	defVal bool,
) (res bool, err error)

func (Map) GetDuration

func (that Map) GetDuration(
	ctx context.Context,
	name string,
	defVal time.Duration,
) (res time.Duration, err error)

func (Map) GetFloat

func (that Map) GetFloat(
	ctx context.Context,
	name string,
	defVal float64,
) (res float64, err error)

func (Map) GetInteger

func (that Map) GetInteger(
	ctx context.Context,
	name string,
	defVal int64,
) (res int64, err error)

func (Map) GetJson

func (that Map) GetJson(
	ctx context.Context,
	name string,
	defVal RawMessage,
) (res RawMessage, err error)

func (Map) GetProperty

func (that Map) GetProperty(
	ctx context.Context,
	name string,
) (interface{}, error)

func (Map) GetString

func (that Map) GetString(
	ctx context.Context,
	name string,
	defVal string,
) (res string, err error)

func (Map) Hash

func (that Map) Hash() (string, error)

func (Map) Json

func (that Map) Json() ([]byte, error)

func (Map) Marshal

func (that Map) Marshal() ([]byte, error)

func (Map) MarshalIndent

func (that Map) MarshalIndent() ([]byte, error)

func (Map) NewScope

func (that Map) NewScope(name string) Map

func (Map) SaveToFile

func (that Map) SaveToFile(filename string) error

func (Map) Scope

func (that Map) Scope(name string) Map

Scope is routine, that allow access to the branch of base Map as sub Map.

func (Map) SetBoolean

func (that Map) SetBoolean(
	ctx context.Context,
	name string,
	value bool,
) error

func (Map) SetDuration

func (that Map) SetDuration(
	ctx context.Context,
	name string,
	value time.Duration,
) error

func (Map) SetFloat

func (that Map) SetFloat(
	ctx context.Context,
	name string,
	value float64,
) error

func (Map) SetInteger

func (that Map) SetInteger(
	ctx context.Context,
	name string,
	value int64,
) error

func (Map) SetJson

func (that Map) SetJson(
	ctx context.Context,
	name string,
	value RawMessage,
) error

func (Map) SetProperty

func (that Map) SetProperty(
	ctx context.Context,
	name string,
	value interface{},
) error

func (Map) SetString

func (that Map) SetString(
	ctx context.Context,
	name string,
	value string,
) error

func (Map) String

func (that Map) String() string

func (Map) ToBoolean

func (that Map) ToBoolean(
	ctx context.Context,
	name string,
	defVal bool,
) bool

func (Map) ToDuration

func (that Map) ToDuration(
	ctx context.Context,
	name string,
	defVal time.Duration,
) time.Duration

func (Map) ToFloat

func (that Map) ToFloat(
	ctx context.Context,
	name string,
	defVal float64,
) float64

func (Map) ToInteger

func (that Map) ToInteger(
	ctx context.Context,
	name string,
	defVal int64,
) int64

func (Map) ToJson

func (that Map) ToJson(
	ctx context.Context,
	name string,
	defVal RawMessage,
) RawMessage

func (Map) ToMap

func (that Map) ToMap(
	ctx context.Context,
	name string,
) Map

func (Map) ToMaps

func (that Map) ToMaps(
	ctx context.Context,
	name string,
) []Map

func (Map) ToSlice

func (that Map) ToSlice(
	ctx context.Context,
	name string,
) []interface{}

func (Map) ToString

func (that Map) ToString(
	ctx context.Context,
	name string,
	defVal string,
) string

func (Map) Unmarshal

func (that Map) Unmarshal(data []byte) error

type Number

type Number = json.Number

type RawMessage

type RawMessage = json.RawMessage

func Extract

func Extract(
	doc []byte,
	path string,
) (RawMessage, error)

Extract is a helper function to extract a value from a JSON document.

func Normalize

func Normalize(data RawMessage) (RawMessage, error)

type String

type String string

String is a custom type for JSON strings.

func (*String) MarshalJSON

func (s *String) MarshalJSON() ([]byte, error)

func (*String) UnmarshalJSON

func (s *String) UnmarshalJSON(data []byte) error

type Strings

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

Strings is a custom type for JSON strings.

func (*Strings) First

func (s *Strings) First() string

func (*Strings) MarshalJSON

func (s *Strings) MarshalJSON() ([]byte, error)

func (*Strings) UnmarshalJSON

func (s *Strings) UnmarshalJSON(data []byte) error

func (*Strings) Values

func (s *Strings) Values() []string

Jump to

Keyboard shortcuts

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