json

package
v0.0.0-...-02bf512 Latest Latest
Warning

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

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

Documentation

Overview

Package json is a representation of a JSON object

SPDX-License-Identifier: Apache-2.0

Index

Constants

This section is empty.

Variables

View Source
var (
	TrueValue  = Value{/* contains filtered or unexported fields */}
	FalseValue = Value{/* contains filtered or unexported fields */}
	NullValue  = Value{/* contains filtered or unexported fields */}
)

Constant values for a invalid, true, false, and null

Functions

func ParsePath

func ParsePath(p string) (fn func(Value) (Value, error), err error)

Convert a path into an Object or Array, such as .addresses[3].city, or [3].city, into a func(Value) Value that performs the lookup on an input Value. If any path part does not exist in the Value passed to the func, an Invalid Value is returned. If the given path is not valid, an error is returned.

Types

type NumberString

type NumberString string

NumberString is a type that allows a plain string to be considered a JSON Number. Allows differentiation between an actual string value, and a string that is really a number value.

type Type

type Type uint

Type is an enum of json types

const (
	Invalid Type = iota
	Object
	Array
	String
	Number
	Boolean
	Null
)

The value types

func (Type) String

func (typ Type) String() string

ToString is the Stringer interface for fmt

type Value

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

Value represents any kind of JSON value - object, array, string, number, boolean, null

func BoolToValue

func BoolToValue(val bool) Value

BoolToValue converts a bool into a Value

func MapToValue

func MapToValue[T map[string]any | map[string]Value](val T) (mval Value, err error)

MapToValue converts a map[string]any to a Value of type Object, where the map values must be: - map[string]any for a sub Object - []any for a sub Array - string for a String - NumberString or any constraint.Numeric type for a Number - bool for a Boolean - nil for a Null

Any other key value results in (Invalid Value, error)

func MustMapToValue

func MustMapToValue[T map[string]any | map[string]Value](val T) Value

MustMapToValue is a must version of MapToValue

func MustNumberToValue

func MustNumberToValue[T NumberString | constraint.Numeric](val T) Value

MustNumberToValue is a must version of NumberToValue

func MustSliceToValue

func MustSliceToValue[T []any | []Value](val T) Value

MustSliceToValue is a must version of SliceToValue

func MustToValue

func MustToValue[T map[string]any | map[string]Value | []any | []Value | string | NumberString | constraint.Numeric | bool](val T) Value

MustToValue is a must version of ToValue

func NumberToValue

func NumberToValue[T NumberString | constraint.Numeric](val T) (Value, error)

NumberToValue converts any numeric type into a Value

func SliceToValue

func SliceToValue[T []any | []Value](val T) (sval Value, err error)

SliceToValue converts a []any to a Value of type Array, where the slice values must be the same as map key values (see MapToValue).

Any other slice value results in the (Invalid Value, error)

func StringToValue

func StringToValue(val string) Value

StringToValue converts a string to a Value

func ToValue

func ToValue[T map[string]any | map[string]Value | []any | []Value | string | NumberString | constraint.Numeric | bool](val T) (res Value, err error)

ToValue converts value types any into a Value

func (Value) AsBool

func (jv Value) AsBool() bool

AsBoolean returns a bool representation of a Value. Panics if the Value is not a boolean.

func (Value) AsMap

func (jv Value) AsMap() map[string]Value

AsMap returns a map representation of a Value. Panics if the Value is not an object.

func (Value) AsNumber

func (jv Value) AsNumber() NumberString

AsNumber returns a NumberString representation of a Value. Panics if the Value is not a number.

func (Value) AsSlice

func (jv Value) AsSlice() []Value

AsSlice returns a slice representation of a Value. Panics if the Value is not an array.

func (Value) AsString

func (jv Value) AsString() string

AsString returns a string representation of a Value. Panics if the Value is not a string, number, or boolean.

func (Value) IsDocument

func (jv Value) IsDocument() bool

IsDocument returns true if a Value is a document (Object or Array)

func (Value) IsNull

func (jv Value) IsNull() bool

IsNull returns true if the Value is a null, else false

func (Value) ToAny

func (jv Value) ToAny() any

ToAny converts the Value to the approriate go type: Object = map[string]any Array = []any String = string Number = NumberString Boolean = bool Null = nil

func (Value) ToMap

func (jv Value) ToMap() map[string]any

ToMap returns a map[string]any representation of a Value. Panics if the Value is not an object.

func (Value) ToSlice

func (jv Value) ToSlice(visitor ...func(Value) any) []any

ToSlice returns a []any representation of a Value. Panics if the Value is not an Array. If the optional visitor func is not provided, then DefaultVisitor is used.

func (Value) Type

func (jv Value) Type() Type

Type returns the type of value this Value contains

Directories

Path Synopsis
Package parse is a streaming parser for JSON objects
Package parse is a streaming parser for JSON objects
Package write writes JSON objects
Package write writes JSON objects

Jump to

Keyboard shortcuts

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