Documentation ¶
Overview ¶
Package jsonutil provides utilities for interacting with a JSON objects.
Index ¶
- func GetString(j JSONToken, path string) (string, error)
- func GetStringOrDefault(j JSONToken, path, def string) (string, error)
- func HasField(j JSONToken, path string) (bool, error)
- func Hash(obj JSONToken, arrayWithoutOrder bool) ([]byte, error)
- func IsIndex(field string) bool
- func JoinPath(segs ...string) string
- func MarshalJSON(tkn JSONToken) string
- func Merge(src JSONToken, dest *JSONToken, failOnOverwrite, overwriteArrays bool) error
- func SegmentPath(path string) ([]string, error)
- func SetField(src JSONToken, field string, dest *JSONToken, overwrite bool, ...) error
- func UnorderedEqual(x, y JSONToken) bool
- type DefaultAccessor
- type JSONArr
- type JSONBool
- type JSONContainer
- type JSONMeta
- type JSONMetaArrayNode
- type JSONMetaContainerNode
- type JSONMetaNode
- func GetNodeField(node JSONMetaNode, path string) (JSONMetaNode, error)
- func GetNodeFieldSegmented(node JSONMetaNode, segments []string) (JSONMetaNode, error)
- func TokenToNode(token JSONToken) (JSONMetaNode, error)
- func TokenToNodeWithProvenance(token JSONToken, key string, cp Provenance) (JSONMetaNode, error)
- type JSONMetaPrimitiveNode
- type JSONNum
- type JSONPrimitive
- type JSONStr
- type JSONToken
- func Deepcopy(t JSONToken) JSONToken
- func GetArray(j JSONToken, path string) ([]JSONToken, error)
- func GetField(src JSONToken, field string) (JSONToken, error)
- func GetFieldSegmented(src JSONToken, segments []string) (JSONToken, error)
- func GetValue(j JSONToken) JSONToken
- func NodeToToken(node JSONMetaNode) (JSONToken, error)
- func StringToToken(text string) *JSONToken
- func UnmarshalJSON(in json.RawMessage) (JSONToken, error)
- type JSONTokenAccessor
- type Provenance
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetString ¶
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 ¶
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 ¶
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 ¶
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 ¶
IsIndex returns true iff the given field looks like [x] where x is any string of any length.
func MarshalJSON ¶
MarshalJSON serializes the given token into JSON string.
func Merge ¶
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 ¶
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 ¶
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.
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 ¶
Equal reports whether j and itself are equal by recursively checking each element in the array.
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).
type JSONBool ¶
type JSONBool bool
JSONBool represents a primitive JSON bool.
type JSONContainer ¶
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).
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 ¶
ContentString prints the meta content as a string.
func (JSONMeta) Key ¶
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 ¶
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 ¶
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.
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.
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 GetArray ¶
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 ¶
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 ¶
GetFieldSegmented is a wrapper for DefaultAccessor.GetFieldSegmented().
func NodeToToken ¶
func NodeToToken(node JSONMetaNode) (JSONToken, error)
NodeToToken converts a JSONMetaNode to a JSONToken, losing the meta data.
func StringToToken ¶
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.