json

package
v0.0.0-...-1b6ad0c Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2020 License: Apache-2.0 Imports: 18 Imported by: 1

Documentation

Index

Constants

This section is empty.

Variables

View Source
var FalseJSONValue = jsonFalse{}

FalseJSONValue is JSON `false`

View Source
var NullJSONValue = jsonNull{}

NullJSONValue is JSON `null`

View Source
var TrueJSONValue = jsonTrue{}

TrueJSONValue is JSON `true`

Functions

func Contains

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

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

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

func EncodeJSON

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

EncodeJSON encodes a JSON value as a sequence of bytes.

func NumInvertedIndexEntries

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

func Pretty(j JSON) (string, error)

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

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

func (b *ArrayBuilder) Add(j JSON)

Add appends JSON to the sequence.

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

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

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

DecodeJSON decodes a value encoded with EncodeJSON.

func DeepInsert

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

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

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

FetchPath implements the #> operator.

func FromBool

func FromBool(v bool) JSON

FromBool returns a JSON value given a bool.

func FromDecimal

func FromDecimal(v apd.Decimal) JSON

FromDecimal returns a JSON value given a apd.Decimal.

func FromEncoding

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

FromEncoding returns a JSON value which is lazily decoded.

func FromFloat64

func FromFloat64(v float64) (JSON, error)

FromFloat64 returns a JSON value given a float64.

func FromInt

func FromInt(v int) JSON

FromInt returns a JSON value given a int.

func FromInt64

func FromInt64(v int64) JSON

FromInt64 returns a JSON value given a int64.

func FromNumber

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

FromNumber returns a JSON value given a json.Number.

func FromString

func FromString(v string) JSON

FromString returns a JSON value given a string.

func MakeJSON

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

func ParseJSON(s string) (JSON, error)

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

func Random

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

Random generates a random 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

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 Type

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.

Jump to

Keyboard shortcuts

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