jsonutil

package
v0.0.0-...-e9b43fb Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2023 License: Apache-2.0 Imports: 12 Imported by: 2

Documentation

Overview

Package jsonutil provides utilities for interacting with a JSON objects.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetString

func GetString(j JSONToken, path string) (string, error)

GetString gets the specified field value for the provided JSON object as a string. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first

func GetStringOrDefault

func GetStringOrDefault(j JSONToken, path, def string) (string, error)

GetStringOrDefault gets the specified field value for the provided JSON object as a string. If no object is found at the specified path, the given default string is returned. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first

func HasField

func HasField(j JSONToken, path string) (bool, error)

HasField determines if the specified field exists and is non-nil for the provided JSON object. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first

func Hash

func Hash(obj JSONToken, arrayWithoutOrder bool) ([]byte, error)

Hash converts the given token into a hash. Key order is not considered. This is not cryptographically secure, and is not to be used for secure hashing. If arrayWithoutOrder is true, array item order will be not considered.

func IsIndex

func IsIndex(field string) bool

IsIndex returns true iff the given field looks like [x] where x is any string of any length.

func JoinPath

func JoinPath(segs ...string) string

JoinPath rejoins the given path segments into a JSON Path.

func MarshalJSON

func MarshalJSON(tkn JSONToken) string

MarshalJSON serializes the given token into JSON string.

func Merge

func Merge(src JSONToken, dest *JSONToken, failOnOverwrite, overwriteArrays bool) error

Merge merges two JSONTokens together. If failOnOverwrite is true, this method guarantees that no existing data anywhere in the destination will be lost (unless overwriteArrays is true).

func SegmentPath

func SegmentPath(path string) ([]string, error)

SegmentPath splits the given JSON path into segments/components. Static path components (like foo.bar.baz are returned verbatim ["foo", "bar", "baz"], and boxed array indices are returned boxed like foo[123].baz => ["foo", "[123]", "baz"]. A numeric static component like foo.3.bar will be returned as a string like ["foo", "3", "bar"].

func SetField

func SetField(src JSONToken, field string, dest *JSONToken, overwrite bool, matchNesting bool) error

SetField sets the specified field of dest JSONToken into the src provided. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first. dest can be of primitive, array or object. if matchNesting is set to True, each element in src will be unpacked at each level and pass to the corresponding field. For example, SetField(0, "foo.baz", &{"foo": {"bar": 1}}, false, false) => {"foo": {"bar": 1, "baz": 0}} SetField(0, "foo.bar", &{"foo": {"bar": 1}}, true, false) => {"foo": {"bar": 0}} SetField(1, "foo[]", &{"foo": [0]}, false, false) => {"foo": [0, 1]} SetField(1, "foo[]", &{"foo": [0]}, false, false) => {"foo": [0, 1]} SetField{[1, 2], "foo[].bar", &{"foo": [0]}, false, false) => {"foo": [0, {"bar": [1, 2]}]} SetField{[1, 2], "foo[].bar", &{"foo": [0]}, false, true) => {"foo": [0, {"bar": 1}, {"bar": 2}]}

func UnorderedEqual

func UnorderedEqual(x, y JSONToken) bool

UnorderedEqual recursively compares two given tokens. Both of key order and array item order are not considered.

Types

type DefaultAccessor

type DefaultAccessor struct{}

DefaultAccessor is a default JSONTokenAccessor to read/write JSONToken and used by the engine Whistler.

func (DefaultAccessor) GetField

func (w DefaultAccessor) GetField(src JSONToken, field string) (JSONToken, error)

GetField gets the specified field value for the provided JSON object.

func (DefaultAccessor) SetField

func (w DefaultAccessor) SetField(src JSONToken, field string, dest *JSONToken, overwrite bool, matchNesting bool) error

SetField sets the specified field value for the provided JSON object.

type JSONArr

type JSONArr []JSONToken

JSONArr is a JSON array that contains 0 or more JSONObjects.

func UnmarshalRawMessages

func UnmarshalRawMessages(resources []*json.RawMessage) (JSONArr, error)

UnmarshalRawMessages unmarshals an array of raw json messages into an array of JSON containers.

func (JSONArr) Equal

func (a JSONArr) Equal(j JSONToken) bool

Equal reports whether j and itself are equal by recursively checking each element in the array.

func (JSONArr) String

func (a JSONArr) String() string

func (*JSONArr) UnmarshalJSON

func (a *JSONArr) UnmarshalJSON(j json.RawMessage) error

UnmarshalJSON sets the contents of the receiver to be the unmarshaled and converted contents of the given JSON (hence given JSON must be of an array, rather than primitive or object).

func (JSONArr) Value

func (a JSONArr) Value() JSONToken

Value gets a JSON array.

type JSONBool

type JSONBool bool

JSONBool represents a primitive JSON bool.

func (JSONBool) Equal

func (b JSONBool) Equal(j JSONToken) bool

Equal reports whether j and itself are equal.

func (JSONBool) Value

func (b JSONBool) Value() JSONToken

Value gets a primitive JSON bool.

type JSONContainer

type JSONContainer map[string]*JSONToken

JSONContainer is a JSON object that contains some child fields. For example, { "foo": "bar", "baz": [1, 2, 3] }

func (JSONContainer) Equal

func (c JSONContainer) Equal(j JSONToken) bool

Equal reports whether j and itself are equal by recursively checking each element in the map.

func (JSONContainer) String

func (c JSONContainer) String() string

func (*JSONContainer) UnmarshalJSON

func (c *JSONContainer) UnmarshalJSON(j json.RawMessage) error

UnmarshalJSON sets the contents of the receiver to be the unmarshaled and converted contents of the given JSON (hence given JSON must be of an object, rather than primitive or array).

func (JSONContainer) Value

func (c JSONContainer) Value() JSONToken

Value gets a JSON object.

type JSONMeta

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

JSONMeta is a container for the common JSONMeta data.

func NewJSONMeta

func NewJSONMeta(key string, cp Provenance) JSONMeta

NewJSONMeta creates a new JSONMeta with the given key and Provenance information.

func (JSONMeta) ContentString

func (jm JSONMeta) ContentString(_ int) string

ContentString prints the meta content as a string.

func (JSONMeta) Key

func (jm JSONMeta) Key() string

Key returns this Meta's key (boxed array index like [1] for array elements, dotted keys like ".foo" for container fields).

func (JSONMeta) Parent

func (jm JSONMeta) Parent() JSONMetaNode

Parent returns this Meta's parent node, if this node was not computationally derived.

func (JSONMeta) Path

func (jm JSONMeta) Path() string

Path returns the full JSON path in dot/bracket notation to this Meta by following its parents.

func (JSONMeta) Provenance

func (jm JSONMeta) Provenance() Provenance

Provenance returns the provenance information of this node.

func (JSONMeta) ProvenanceString

func (jm JSONMeta) ProvenanceString() string

ProvenanceString produces a string representing the approximate provenance of this JSONMeta. This uses Provenance.ShallowString and thus may contain incomplete information. It shall not include any content of the data.

type JSONMetaArrayNode

type JSONMetaArrayNode struct {
	// Value used intentionally to avoid modification.
	JSONMeta
	Items []JSONMetaNode
}

JSONMetaArrayNode is a JSONMetaNode with an additional Array value.

func (JSONMetaArrayNode) ContentString

func (j JSONMetaArrayNode) ContentString(level int) string

ContentString produces a string representation of the contents of JSONMetaArrayNode.

func (JSONMetaArrayNode) String

func (j JSONMetaArrayNode) String() string

String produces a string representation of JSONMetaArrayNode, including its path.

type JSONMetaContainerNode

type JSONMetaContainerNode struct {
	// Value used intentionally to avoid modification.
	JSONMeta
	Children map[string]JSONMetaNode
}

JSONMetaContainerNode is a JSONMetaNode with an additional Children value.

func (JSONMetaContainerNode) ContentString

func (j JSONMetaContainerNode) ContentString(level int) string

ContentString produces a string representation of the contents of JSONMetaContainerNode.

func (JSONMetaContainerNode) String

func (j JSONMetaContainerNode) String() string

String produces a string representation of JSONMetaContainerNode, including its path.

type JSONMetaNode

type JSONMetaNode interface {
	Parent() JSONMetaNode
	Key() string
	Path() string
	ContentString(level int) string
	Provenance() Provenance
	ProvenanceString() string
}

JSONMetaNode represents a JSONToken with at least a Parent and Key, and some way to derive a full JSON dot/bracket notation path.

func GetNodeField

func GetNodeField(node JSONMetaNode, path string) (JSONMetaNode, error)

GetNodeField returns the child given a path in dot/bracket notation like foo.bar[3].baz

func GetNodeFieldSegmented

func GetNodeFieldSegmented(node JSONMetaNode, segments []string) (JSONMetaNode, error)

GetNodeFieldSegmented returns the child given a path in parsed dot/bracket notation like ["foo", "bar", "[3]", "baz"]

func TokenToNode

func TokenToNode(token JSONToken) (JSONMetaNode, error)

TokenToNode converts a JSONToken to a JSONMetaNode, filling the meta data.

func TokenToNodeWithProvenance

func TokenToNodeWithProvenance(token JSONToken, key string, cp Provenance) (JSONMetaNode, error)

TokenToNodeWithProvenance converts a JSONToken to a JSONMetaNode, filling the meta data with the given provenance.

type JSONMetaPrimitiveNode

type JSONMetaPrimitiveNode struct {
	// Value used intentionally to avoid modification.
	JSONMeta
	Value JSONPrimitive
}

JSONMetaPrimitiveNode is a JSONMetaNode with an additional primitive value.

func (JSONMetaPrimitiveNode) ContentString

func (j JSONMetaPrimitiveNode) ContentString(_ int) string

ContentString produces a string representation of the contents of JSONMetaPrimitiveNode.

func (JSONMetaPrimitiveNode) String

func (j JSONMetaPrimitiveNode) String() string

String produces a string representation of JSONMetaPrimitiveNode, including its path.

type JSONNum

type JSONNum float64

JSONNum represents a primitive JSON float.

func (JSONNum) Equal

func (n JSONNum) Equal(j JSONToken) bool

Equal reports whether j and itself are equal.

func (JSONNum) Value

func (n JSONNum) Value() JSONToken

Value gets a primitive JSON float.

type JSONPrimitive

type JSONPrimitive interface {
	// contains filtered or unexported methods
}

JSONPrimitive represents one of the JSON primitive types (Str, Bool, Num). This is useful for defining a variable/parameter that is guaranteed to be a leaf node in the JSON tree.

type JSONStr

type JSONStr string

JSONStr represents a primitive JSON string.

func (JSONStr) Equal

func (s JSONStr) Equal(j JSONToken) bool

Equal reports whether j and itself are equal.

func (JSONStr) Value

func (s JSONStr) Value() JSONToken

Value gets a primitive JSON string.

type JSONToken

type JSONToken interface {
	Value() JSONToken
	Equal(JSONToken) bool
	// contains filtered or unexported methods
}

JSONToken represents a base JSON token. It can be a primitive, an array, or a container object.

func Deepcopy

func Deepcopy(t JSONToken) JSONToken

Deepcopy will make a deep copy of the elements inside a JSONToken

func GetArray

func GetArray(j JSONToken, path string) ([]JSONToken, error)

GetArray gets the specified array value for the provided JSON object. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first

func GetField

func GetField(src JSONToken, field string) (JSONToken, error)

GetField gets the specified field value for the provided JSON object. Nested fields can be accessed using the "." notation and repeated fields can be accessed using the "[i]" notation. E.g. name[0].first If the field is not found, nil is returned. Please note that field string should not have "[]" suffix. For example, GetField({"foo": "bar"}, "") => {"foo": "bar"} GetField(123, "") => 123 GetField({"foo": "bar"}, "foo") => "bar" GetField({"foo": ["bar", 1]}, "foo") => ["bar", 1] GetField({"foo": ["bar", 1]}, "foo[0]") => "bar" GetField({"foo": [{"bar": 1}, {"bar": 2}], "foo[*].bar") => [1, 2] GetField is a wrapper for DefaultAccessor.GetField().

func GetFieldSegmented

func GetFieldSegmented(src JSONToken, segments []string) (JSONToken, error)

GetFieldSegmented is a wrapper for DefaultAccessor.GetFieldSegmented().

func GetValue

func GetValue(j JSONToken) JSONToken

GetValue gets actual JSON value in a JSONToken. If it is nil, return nil.

func NodeToToken

func NodeToToken(node JSONMetaNode) (JSONToken, error)

NodeToToken converts a JSONMetaNode to a JSONToken, losing the meta data.

func StringToToken

func StringToToken(text string) *JSONToken

StringToToken returns a pointer to a JSONToken created using the input string.

func UnmarshalJSON

func UnmarshalJSON(in json.RawMessage) (JSONToken, error)

UnmarshalJSON determines the type of the RawMessage and unmarshals it into a JSONToken.

type JSONTokenAccessor

type JSONTokenAccessor interface {
	GetField(src JSONToken, field string) (JSONToken, error)
	SetField(src JSONToken, field string, dest *JSONToken, forceOverwrite bool, matchNesting bool) error
	// contains filtered or unexported methods
}

JSONTokenAccessor defines an interface for accessing JSONToken with different engines.

type Provenance

type Provenance struct {
	Sources  []JSONMetaNode
	Function string
}

Provenance contains information on the source provenance of this node if it was created by the computation of some other nodes going through a function.

func (Provenance) ShallowString

func (c Provenance) ShallowString() string

ShallowString returns the paths of the arguments and the function used to get this node. It does not recursively check for provenance of the parent nodes.

Jump to

Keyboard shortcuts

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