cockroach: github.com/cockroachdb/cockroach/pkg/util/json Index | Files

package json

import "github.com/cockroachdb/cockroach/pkg/util/json"

Index

Package Files

contains.go contains_testers.go encode.go encoded.go iterator.go jentry.go json.go random.go tables.go

Variables

var FalseJSONValue = jsonFalse{}

FalseJSONValue is JSON `false`

var NullJSONValue = jsonNull{}

NullJSONValue is JSON `null`

var TrueJSONValue = jsonTrue{}

TrueJSONValue is JSON `true`

func Contains Uses

func Contains(a, b JSON) (bool, error)

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 Uses

func EncodeInvertedIndexKeys(b []byte, json JSON) ([][]byte, error)

EncodeInvertedIndexKeys takes in a key prefix and returns a slice of inverted index keys, one per path through the receiver.

func EncodeJSON Uses

func EncodeJSON(appendTo []byte, j JSON) ([]byte, error)

EncodeJSON encodes a JSON value as a sequence of bytes.

func NumInvertedIndexEntries Uses

func NumInvertedIndexEntries(j JSON) (int, error)

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.

func Pretty Uses

func Pretty(j JSON) (string, error)

Pretty pretty-prints the given JSON document as required by jsonb_pretty.

type ArrayBuilder Uses

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

ArrayBuilder builds JSON Array by a JSON sequence.

func NewArrayBuilder Uses

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) Add Uses

func (b *ArrayBuilder) Add(j JSON)

Add appends JSON to the sequence.

func (*ArrayBuilder) Build Uses

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 Uses

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

ArrayBuilderWithCounter builds JSON Array by a JSON sequence with a size counter.

func NewArrayBuilderWithCounter Uses

func NewArrayBuilderWithCounter() *ArrayBuilderWithCounter

NewArrayBuilderWithCounter returns an ArrayBuilderWithCounter.

func (*ArrayBuilderWithCounter) Add Uses

func (b *ArrayBuilderWithCounter) Add(j JSON)

Add appends JSON to the sequence and updates the size counter.

func (*ArrayBuilderWithCounter) Build Uses

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 Uses

func (b *ArrayBuilderWithCounter) Size() uintptr

Size returns the size in bytes of the JSON Array the builder is going to build.

type JSON Uses

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 Uses

func AllPaths(j JSON) ([]JSON, error)

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 Uses

func DecodeJSON(b []byte) ([]byte, JSON, error)

DecodeJSON decodes a value encoded with EncodeJSON.

func DeepInsert Uses

func DeepInsert(j JSON, path []string, to JSON, insertAfter bool) (JSON, error)

DeepInsert inserts a value at a path in a JSON document. Implements the jsonb_insert builtin.

func DeepSet Uses

func DeepSet(j JSON, path []string, to JSON, createMissing bool) (JSON, error)

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 FetchPath Uses

func FetchPath(j JSON, path []string) (JSON, error)

FetchPath implements the #> operator.

func FromBool Uses

func FromBool(v bool) JSON

FromBool returns a JSON value given a bool.

func FromDecimal Uses

func FromDecimal(v apd.Decimal) JSON

FromDecimal returns a JSON value given a apd.Decimal.

func FromEncoding Uses

func FromEncoding(b []byte) (JSON, error)

FromEncoding returns a JSON value which is lazily decoded.

func FromFloat64 Uses

func FromFloat64(v float64) (JSON, error)

FromFloat64 returns a JSON value given a float64.

func FromInt Uses

func FromInt(v int) JSON

FromInt returns a JSON value given a int.

func FromInt64 Uses

func FromInt64(v int64) JSON

FromInt64 returns a JSON value given a int64.

func FromNumber Uses

func FromNumber(v json.Number) (JSON, error)

FromNumber returns a JSON value given a json.Number.

func FromString Uses

func FromString(v string) JSON

FromString returns a JSON value given a string.

func MakeJSON Uses

func MakeJSON(d interface{}) (JSON, error)

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 Uses

func ParseJSON(s string) (JSON, error)

ParseJSON takes a string of JSON and returns a JSON value.

func Random Uses

func Random(complexity int, rng *rand.Rand) (JSON, error)

Random generates a random JSON value.

type ObjectBuilder Uses

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

ObjectBuilder builds JSON Object by a key value pair sequence.

func NewObjectBuilder Uses

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 Uses

func (b *ObjectBuilder) Add(k string, v JSON)

Add appends key value pair to the sequence.

func (*ObjectBuilder) Build Uses

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 ObjectIterator Uses

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 Uses

func (it *ObjectIterator) Key() string

Key returns key of the current pair.

func (*ObjectIterator) Next Uses

func (it *ObjectIterator) Next() bool

Next updates the cursor and returns whether the next pair exists.

func (*ObjectIterator) Value Uses

func (it *ObjectIterator) Value() JSON

Value returns value of the current pair

type Type Uses

type Type int

Type represents a JSON type.

const (
    NullJSONType Type
    StringJSONType
    NumberJSONType
    FalseJSONType
    TrueJSONType
    ArrayJSONType
    ObjectJSONType
)

This enum defines the ordering of types. It should not be reordered.

Package json imports 17 packages (graph) and is imported by 20 packages. Updated 2019-09-17. Refresh now. Tools for package owners.