Documentation ¶
Index ¶
- Variables
- func Contains(a, b JSON) (bool, 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 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 FromString(v string) JSON
- func MakeJSON(d interface{}) (JSON, error)
- func ParseJSON(s string) (JSON, error)
- func Random(complexity int, rng *rand.Rand) (JSON, error)
- type ObjectBuilder
- type ObjectBuilderWithCounter
- type ObjectIterator
- 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 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 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 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 // 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) // Exists implements the `?` operator. 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 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{}.
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