Documentation ¶
Index ¶
- Variables
- func AsStringWithErrorChance(j JSON, rng *rand.Rand, p float32) string
- func Contains(a, b JSON) (bool, error)
- func EncodeContainedInvertedIndexSpans(b []byte, json JSON) (invertedExpr inverted.Expression, err error)
- func EncodeContainingInvertedIndexSpans(b []byte, json JSON) (invertedExpr inverted.Expression, err error)
- func EncodeExistsInvertedIndexSpans(b []byte, s string) (invertedExpr inverted.Expression, err error)
- func EncodeInvertedIndexKeys(b []byte, json JSON) ([][]byte, error)
- func EncodeJSON(appendTo []byte, j JSON) ([]byte, error)
- func NumInvertedIndexEntries(j JSON) (int, error)
- func Pretty(j JSON) (string, error)
- type ArrayBuilder
- type ArrayBuilderWithCounter
- type FixedKeysObjectBuilder
- type JSON
- func AllPaths(j JSON) ([]JSON, error)
- func DecodeJSON(b []byte) ([]byte, JSON, error)
- func DeepInsert(j JSON, path []string, to JSON, insertAfter bool) (JSON, error)
- func DeepSet(j JSON, path []string, to JSON, createMissing bool) (JSON, error)
- func FetchPath(j JSON, path []string) (JSON, error)
- func FromBool(v bool) JSON
- func FromDecimal(v apd.Decimal) JSON
- func FromEncoding(b []byte) (JSON, error)
- func FromFloat64(v float64) (JSON, error)
- func FromInt(v int) JSON
- func FromInt64(v int64) JSON
- func FromNumber(v json.Number) (JSON, error)
- func FromSpatialObject(so geopb.SpatialObject, numDecimalDigits int) (JSON, error)
- func FromString(v string) JSON
- func MakeJSON(d interface{}) (JSON, error)
- func ParseJSON(s string, opts ...ParseOption) (JSON, error)
- func RandGen(rng *rand.Rand, opts ...RandOption) (JSON, error)
- func Random(complexity int, rng *rand.Rand) (JSON, error)
- type ObjectBuilder
- type ObjectBuilderWithCounter
- type ObjectIterator
- type ParseOption
- type RandOption
- type Type
Constants ¶
This section is empty.
Variables ¶
var FalseJSONValue = jsonFalse{}
FalseJSONValue is JSON `false`
var NullJSONValue = jsonNull{}
NullJSONValue is JSON `null`
var TrueJSONValue = jsonTrue{}
TrueJSONValue is JSON `true`
Functions ¶
func AsStringWithErrorChance ¶
AsStringWithErrorChance returns string representation of JSON object, but allows up to specified chance that the returned string will contain errors -- i.e. the string should not be parse-able back into JSON.
func Contains ¶
Contains returns true if a contains b. This implements the @>, <@ operators. See the Postgres docs for the expected semantics of Contains. https://www.postgresql.org/docs/10/static/datatype-json.html#JSON-CONTAINMENT The naive approach to doing array containment would be to do an O(n^2) nested loop through the arrays to check if one is contained in the other. We're out of luck when the arrays contain other arrays or objects (there might actually be something fancy we can do, but there's nothing obvious). When the arrays contain scalars however, we can optimize this by pre-sorting both arrays and iterating through them in lockstep. To this end, we preprocess the JSON document to sort all of its arrays so that when we perform contains we can extract the scalars sorted, and then also the arrays and objects in separate arrays, so that we can do the fast thing for the subset of the arrays which are scalars.
func EncodeContainedInvertedIndexSpans ¶
func EncodeContainedInvertedIndexSpans( b []byte, json JSON, ) (invertedExpr inverted.Expression, err error)
EncodeContainedInvertedIndexSpans takes in a key prefix and returns the spans that must be scanned in the inverted index to evaluate a contained by (<@) predicate with the given JSON (i.e., find the objects in the index that could be contained by the given JSON).
The spans are returned in an inverted.SpanExpression, which represents the set operations that must be applied on the spans read during execution. See comments in the SpanExpression definition for details.
The input inKey is prefixed to the keys in all returned spans.
func EncodeContainingInvertedIndexSpans ¶
func EncodeContainingInvertedIndexSpans( b []byte, json JSON, ) (invertedExpr inverted.Expression, err error)
EncodeContainingInvertedIndexSpans takes in a key prefix and returns the spans that must be scanned in the inverted index to evaluate a contains (@>) predicate with the given JSON (i.e., find the objects in the index that contain the given JSON).
The spans are returned in an inverted.SpanExpression, which represents the set operations that must be applied on the spans read during execution. See comments in the SpanExpression definition for details.
The input inKey is prefixed to the keys in all returned spans.
func EncodeExistsInvertedIndexSpans ¶
func EncodeExistsInvertedIndexSpans( b []byte, s string, ) (invertedExpr inverted.Expression, err error)
EncodeExistsInvertedIndexSpans takes in a key prefix and returns the spans that must be scanned in the inverted index to evaluate an exists (?) predicate with the given JSON (i.e., find the objects/arrays in the index that contain the given string as a key, or *are* the given string).
The spans are returned in an inverted.SpanExpression, which represents the set operations that must be applied on the spans read during execution. See comments in the SpanExpression definition for details.
The input inKey is prefixed to the keys in all returned spans.
func EncodeInvertedIndexKeys ¶
EncodeInvertedIndexKeys takes in a key prefix and returns a slice of inverted index keys, one per unique path through the receiver.
func EncodeJSON ¶
EncodeJSON encodes a JSON value as a sequence of bytes.
func NumInvertedIndexEntries ¶
NumInvertedIndexEntries returns the number of inverted index entries that would be created for the given JSON value. Since identical elements of an array are encoded identically in the inverted index, the total number of distinct index entries may be less than the total number of paths.
Types ¶
type ArrayBuilder ¶
type ArrayBuilder struct {
// contains filtered or unexported fields
}
ArrayBuilder builds JSON Array by a JSON sequence.
func NewArrayBuilder ¶
func NewArrayBuilder(numAddsHint int) *ArrayBuilder
NewArrayBuilder returns an ArrayBuilder. The builder will reserve spaces based on hint about number of adds to reduce times of growing capacity.
func (*ArrayBuilder) Build ¶
func (b *ArrayBuilder) Build() JSON
Build returns the constructed JSON array. A caller may not modify the array, and the ArrayBuilder reserves the right to re-use the array returned (though the data will not be modified). This is important in the case of a window function, which might want to incrementally update an aggregation.
type ArrayBuilderWithCounter ¶
type ArrayBuilderWithCounter struct {
// contains filtered or unexported fields
}
ArrayBuilderWithCounter builds JSON Array by a JSON sequence with a size counter.
func NewArrayBuilderWithCounter ¶
func NewArrayBuilderWithCounter() *ArrayBuilderWithCounter
NewArrayBuilderWithCounter returns an ArrayBuilderWithCounter.
func (*ArrayBuilderWithCounter) Add ¶
func (b *ArrayBuilderWithCounter) Add(j JSON)
Add appends JSON to the sequence and updates the size counter.
func (*ArrayBuilderWithCounter) Build ¶
func (b *ArrayBuilderWithCounter) Build() JSON
Build returns a JSON array built from a JSON sequence. After that, it should not be modified any longer.
func (*ArrayBuilderWithCounter) Size ¶
func (b *ArrayBuilderWithCounter) Size() uintptr
Size returns the size in bytes of the JSON Array the builder is going to build.
type FixedKeysObjectBuilder ¶
type FixedKeysObjectBuilder struct {
// contains filtered or unexported fields
}
FixedKeysObjectBuilder is a JSON object builder that builds an object for the specified fixed set of unique keys. This object can be reused to build multiple instances of JSON object.
func NewFixedKeysObjectBuilder ¶
func NewFixedKeysObjectBuilder(keys []string) (*FixedKeysObjectBuilder, error)
NewFixedKeysObjectBuilder creates JSON object builder for the specified set of fixed, unique keys.
func (*FixedKeysObjectBuilder) Build ¶
func (b *FixedKeysObjectBuilder) Build() (JSON, error)
Build builds JSON object.
type JSON ¶
type JSON interface { fmt.Stringer Compare(JSON) (int, error) // Type returns the JSON type. Type() Type // Format writes out the JSON document to the specified buffer. Format(buf *bytes.Buffer) // Size returns the size of the JSON document in bytes. Size() uintptr // EncodeForwardIndex implements forward indexing for JSONB values. // The encoding depends on the direction of the encoding // specified, using `dir`, and is appended to `buf` and returned. EncodeForwardIndex(buf []byte, dir encoding.Direction) ([]byte, error) // FetchValKey implements the `->` operator for strings, returning nil if the // key is not found. FetchValKey(key string) (JSON, error) // FetchValIdx implements the `->` operator for ints, returning nil if the // key is not found. FetchValIdx(idx int) (JSON, error) // FetchValKeyOrIdx is used for path access, if obj is an object, it tries to // access the given field. If it's an array, it interprets the key as an int // and tries to access the given index. FetchValKeyOrIdx(key string) (JSON, error) // RemoveString implements the `-` operator for strings, returning JSON after removal, // whether removal is valid and error message. RemoveString(s string) (JSON, bool, error) // RemoveIndex implements the `-` operator for ints, returning JSON after removal, // whether removal is valid and error message. RemoveIndex(idx int) (JSON, bool, error) // RemovePath and doRemovePath implement the `#-` operator for strings, returning JSON after removal, // whether removal is valid and error message. RemovePath(path []string) (JSON, bool, error) // Concat implements the `||` operator. Concat(other JSON) (JSON, error) // AsText returns the JSON document as a string, with quotes around strings removed, and null as nil. AsText() (*string, error) // AsDecimal returns the JSON document as a apd.Decimal if it is a numeric // type, and a boolean indicating if this JSON value is a numeric type. AsDecimal() (*apd.Decimal, bool) // AsBool returns the JSON document as a boolean if it is a boolean type, // and a boolean indicating if this JSON value is a bool type. AsBool() (bool, bool) // AsArray returns the JSON document as an Array if it is a array type, // and a boolean indicating if this JSON value is a array type. AsArray() ([]JSON, bool) // AreKeysSorted returns if the keys in a JSON Object are sorted by // increasing order. It returns false if the underlying JSON value // is not a JSON object. AreKeysSorted() bool // Exists implements the `?` operator: does the string exist as a top-level // key within the JSON value? // // If the object is a JSON array, returns true when the key is a top-level // element of the array. Exists(string) (bool, error) // StripNulls returns the JSON document with all object fields that have null values omitted // and whether it needs to strip nulls. Stripping nulls is needed only if it contains some // object fields having null values. StripNulls() (JSON, bool, error) // ObjectIter returns an *ObjectKeyIterator, nil if json is not an object. ObjectIter() (*ObjectIterator, error) // MaybeDecode returns an equivalent JSON which is not a jsonEncoded. MaybeDecode() JSON // Len returns the number of outermost elements in the JSON document if it is an object or an array. // Otherwise, Len returns 0. Len() int // HasContainerLeaf returns whether this document contains in it somewhere // either the empty array or the empty object. HasContainerLeaf() (bool, error) // contains filtered or unexported methods }
JSON represents a JSON value.
func AllPaths ¶
AllPaths returns a slice of new JSON documents, each a path to a leaf through the input. Note that leaves include the empty object and array in addition to scalars.
func DecodeJSON ¶
DecodeJSON decodes a value encoded with EncodeJSON.
func DeepInsert ¶
DeepInsert inserts a value at a path in a JSON document. Implements the jsonb_insert builtin.
func DeepSet ¶
DeepSet sets a path to a value in a JSON document. Largely follows the same semantics as setValKeyOrIdx, but with a path. Implements the jsonb_set builtin.
func FromDecimal ¶
func FromDecimal(v apd.Decimal) JSON
FromDecimal returns a JSON value given a apd.Decimal.
func FromEncoding ¶
FromEncoding returns a JSON value which is lazily decoded.
func FromFloat64 ¶
FromFloat64 returns a JSON value given a float64.
func FromNumber ¶
FromNumber returns a JSON value given a json.Number.
func FromSpatialObject ¶
func FromSpatialObject(so geopb.SpatialObject, numDecimalDigits int) (JSON, error)
FromSpatialObject transforms a SpatialObject into the json.JSON type.
func MakeJSON ¶
MakeJSON returns a JSON value given a Go-style representation of JSON. * JSON null is Go `nil`, * JSON true is Go `true`, * JSON false is Go `false`, * JSON numbers are json.Number | int | int64 | float64, * JSON string is a Go string, * JSON array is a Go []interface{}, * JSON object is a Go map[string]interface{}.
func ParseJSON ¶
func ParseJSON(s string, opts ...ParseOption) (JSON, error)
ParseJSON takes a string of JSON and returns a JSON value.
type ObjectBuilder ¶
type ObjectBuilder struct {
// contains filtered or unexported fields
}
ObjectBuilder builds JSON Object by a key value pair sequence.
func NewObjectBuilder ¶
func NewObjectBuilder(numAddsHint int) *ObjectBuilder
NewObjectBuilder returns an ObjectBuilder. The builder will reserve spaces based on hint about number of adds to reduce times of growing capacity.
func (*ObjectBuilder) Add ¶
func (b *ObjectBuilder) Add(k string, v JSON)
Add appends key value pair to the sequence.
func (*ObjectBuilder) Build ¶
func (b *ObjectBuilder) Build() JSON
Build returns a JSON object built from a key value pair sequence. After that, it should not be modified any longer.
type ObjectBuilderWithCounter ¶
type ObjectBuilderWithCounter struct {
// contains filtered or unexported fields
}
ObjectBuilderWithCounter builds a JSON object a key/value pair at a time, keeping the memory usage of the object.
func NewObjectBuilderWithCounter ¶
func NewObjectBuilderWithCounter() *ObjectBuilderWithCounter
NewObjectBuilderWithCounter creates and instantiates ObjectBuilder with memory counter.
func (*ObjectBuilderWithCounter) Add ¶
func (b *ObjectBuilderWithCounter) Add(k string, v JSON)
Add appends key value pair to the sequence and updates amount of memory allocated for the overall keys and values.
func (*ObjectBuilderWithCounter) Build ¶
func (b *ObjectBuilderWithCounter) Build() JSON
Build returns a JSON object built from a key value pair sequence. After that, it should not be modified any longer.
func (*ObjectBuilderWithCounter) Size ¶
func (b *ObjectBuilderWithCounter) Size() uintptr
Size returns the size in bytes of the JSON object the builder is going to build.
type ObjectIterator ¶
type ObjectIterator struct {
// contains filtered or unexported fields
}
ObjectIterator is an iterator to access the key value pair of an object in sorted order based on key.
func (*ObjectIterator) Key ¶
func (it *ObjectIterator) Key() string
Key returns key of the current pair.
func (*ObjectIterator) Next ¶
func (it *ObjectIterator) Next() bool
Next updates the cursor and returns whether the next pair exists.
func (*ObjectIterator) Value ¶
func (it *ObjectIterator) Value() JSON
Value returns value of the current pair
type ParseOption ¶
type ParseOption interface {
// contains filtered or unexported methods
}
ParseOption is an option for JSON parsing.
func WithFastJSONParser ¶
func WithFastJSONParser() ParseOption
WithFastJSONParser returns an option that forces the use of fast json parser.
func WithGoStandardParser ¶
func WithGoStandardParser() ParseOption
WithGoStandardParser returns an option that forces the use of Go parser (encoding/json).
func WithUnorderedObjectKeys ¶
func WithUnorderedObjectKeys() ParseOption
WithUnorderedObjectKeys returns an option that leaves JSON object keys unordered.
type RandOption ¶
type RandOption interface {
// contains filtered or unexported methods
}
RandOption is an option to control generation of random json.
func WithComplexity ¶
func WithComplexity(c int) RandOption
WithComplexity returns an option to set maximum complexity of JSON objects.
func WithEscapeProb ¶
func WithEscapeProb(p float32) RandOption
WithEscapeProb returns an option that configures the probability of producing escaped character in JSON. Setting to 0 produces JSON strings consisting of printable characters only.
func WithMaxStrLen ¶
func WithMaxStrLen(l int) RandOption
WithMaxStrLen returns an option to set maximum length of random JSON string objects.