ast

package
v1.12.5 Latest Latest
Warning

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

Go to latest
Published: Nov 20, 2024 License: Apache-2.0 Imports: 21 Imported by: 31

Documentation

Index

Constants

View Source
const (
	V_NONE   = 0
	V_ERROR  = 1
	V_NULL   = int(types.V_NULL)
	V_TRUE   = int(types.V_TRUE)
	V_FALSE  = int(types.V_FALSE)
	V_ARRAY  = int(types.V_ARRAY)
	V_OBJECT = int(types.V_OBJECT)
	V_STRING = int(types.V_STRING)
	V_NUMBER = int(_V_NUMBER)
	V_ANY    = int(_V_ANY)
)

Variables

View Source
var (
	// ErrNotExist means both key and value doesn't exist
	ErrNotExist error = newError(_ERR_NOT_FOUND, "value not exists")

	// ErrUnsupportType means API on the node is unsupported
	ErrUnsupportType error = newError(_ERR_UNSUPPORT_TYPE, "unsupported type")
)
View Source
var VisitOPSkip = errors.New("")

If visitor return this error on `OnObjectBegin()` or `OnArrayBegin()`, the transverer will skip entiry object or array

Functions

func Loads

func Loads(src string) (int, interface{}, error)

Loads parse all json into interface{}

func LoadsUseNumber

func LoadsUseNumber(src string) (int, interface{}, error)

LoadsUseNumber parse all json into interface{}, with numeric nodes casted to json.Number

func Preorder added in v1.10.0

func Preorder(str string, visitor Visitor, opts *VisitorOptions) error

Preorder decodes the whole JSON string and callbacks each AST node to visitor during preorder traversal. Any visitor method with an error returned will break the traversal and the given error will be directly returned. The opts argument can be reused after every call.

Types

type Iterator

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

func (*Iterator) HasNext

func (self *Iterator) HasNext() bool

HasNext reports if it is the end of iteration or has error.

func (*Iterator) Len

func (self *Iterator) Len() int

func (*Iterator) Pos

func (self *Iterator) Pos() int

type ListIterator

type ListIterator struct {
	Iterator
}

ListIterator is specialized iterator for V_ARRAY

func (*ListIterator) Next

func (self *ListIterator) Next(v *Node) bool

Next scans through children of underlying V_ARRAY, copies each child to v, and returns .HasNext().

type Node

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

func NewAny

func NewAny(any interface{}) Node

NewAny creates a node of type V_ANY if any's type isn't Node or *Node, which stores interface{} and can be only used for `.Interface()`\`.MarshalJSON()`.

func NewArray

func NewArray(v []Node) Node

NewArray creates a node of type V_ARRAY, using v as its underlying children

func NewBool

func NewBool(v bool) Node

NewBool creates a node of type bool:

If v is true, returns V_TRUE node
If v is false, returns V_FALSE node

func NewBytes

func NewBytes(src []byte) Node

NewBytes encodes given src with Base64 (RFC 4648), and creates a node of type V_STRING.

func NewNull

func NewNull() Node

NewNull creates a node of type V_NULL

func NewNumber

func NewNumber(v string) Node

NewNumber creates a json.Number node v must be a decimal string complying with RFC8259

func NewObject

func NewObject(v []Pair) Node

NewObject creates a node of type V_OBJECT, using v as its underlying children

func NewRaw

func NewRaw(json string) Node

NewRaw creates a node of raw json. If the input json is invalid, NewRaw returns a error Node.

func NewRawConcurrentRead added in v1.12.0

func NewRawConcurrentRead(json string) Node

NewRawConcurrentRead creates a node of raw json, which can be READ (GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON) concurrently. If the input json is invalid, NewRaw returns a error Node.

func NewString

func NewString(v string) Node

NewString creates a node of type V_STRING. v is considered to be a valid UTF-8 string, which means it won't be validated and unescaped. when the node is encoded to json, v will be escaped.

func (*Node) Add

func (self *Node) Add(node Node) error

Add appends the given node under self.

If self is V_NONE or V_NULL, it becomes V_ARRAY and sets the node at index 0.

func (*Node) AddAny

func (self *Node) AddAny(val interface{}) error

SetAny wraps val with V_ANY node, and Add() the node.

func (*Node) Array

func (self *Node) Array() ([]interface{}, error)

Array loads all indexes of an array node

func (*Node) ArrayUseNode

func (self *Node) ArrayUseNode() ([]Node, error)

ArrayUseNode copys both parsed and non-parsed chidren nodes, and indexes them by original order

func (*Node) ArrayUseNumber

func (self *Node) ArrayUseNumber() ([]interface{}, error)

ArrayUseNumber loads all indexes of an array node, with numeric nodes casted to json.Number

func (*Node) Bool

func (self *Node) Bool() (bool, error)

Bool returns bool value represented by this node, including types.V_TRUE|V_FALSE|V_NUMBER|V_STRING|V_ANY|V_NULL, V_NONE will return error

func (*Node) Cap

func (self *Node) Cap() (int, error)

Cap returns malloc capacity of a array|object node for children

func (*Node) Check

func (self *Node) Check() error

Check checks if the node itself is valid, and return:

  • ErrNotExist If the node is nil
  • Its underlying error If the node is V_ERROR

func (Node) Error

func (self Node) Error() string

Error returns error message if the node is invalid

func (*Node) Exists

func (self *Node) Exists() bool

Exists returns false only if the self is nil or empty node V_NONE

func (*Node) Float64

func (self *Node) Float64() (float64, error)

Float64 cast node to float64, including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, V_NONE it will return error

func (*Node) ForEach

func (self *Node) ForEach(sc Scanner) error

ForEach scans one V_OBJECT node's children from JSON head to tail, and pass the Sequence and Node of corresponding JSON value.

Especailly, if the node is not V_ARRAY or V_OBJECT, the node itself will be returned and Sequence.Index == -1.

NOTICE: A unsetted node WON'T trigger sc, but its index still counts into Path.Index

func (*Node) Get

func (self *Node) Get(key string) *Node

Get loads given key of an object node on demands

func (*Node) GetByPath

func (self *Node) GetByPath(path ...interface{}) *Node

GetByPath load given path on demands, which only ensure nodes before this path got parsed.

Note, the api expects the json is well-formed at least, otherwise it may return unexpected result.

func (*Node) Index

func (self *Node) Index(idx int) *Node

Index indexies node at given idx, node type CAN be either V_OBJECT or V_ARRAY

func (*Node) IndexOrGet

func (self *Node) IndexOrGet(idx int, key string) *Node

IndexOrGet firstly use idx to index a value and check if its key matches If not, then use the key to search value

func (*Node) IndexOrGetWithIdx added in v1.11.1

func (self *Node) IndexOrGetWithIdx(idx int, key string) (*Node, int)

IndexOrGetWithIdx attempts to retrieve a node by index and key, returning the node and its correct index. If the key does not match at the given index, it searches by key and returns the node with its updated index.

func (*Node) IndexPair

func (self *Node) IndexPair(idx int) *Pair

IndexPair indexies pair at given idx, node type MUST be either V_OBJECT

func (*Node) Int64

func (self *Node) Int64() (int64, error)

Int64 casts the node to int64 value, including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING V_NONE it will return error

func (*Node) Interface

func (self *Node) Interface() (interface{}, error)

Interface loads all children under all pathes from this node, and converts itself as generic type. WARN: all numberic nodes are casted to float64

func (*Node) InterfaceUseNode

func (self *Node) InterfaceUseNode() (interface{}, error)

InterfaceUseNode clone itself as a new node, or its children as map[string]Node (or []Node)

func (*Node) InterfaceUseNumber

func (self *Node) InterfaceUseNumber() (interface{}, error)

InterfaceUseNumber works same with Interface() except numberic nodes are casted to json.Number

func (Node) IsRaw deprecated

func (self Node) IsRaw() bool

isRaw returns true if node's underlying value is raw json

Deprecated: not concurent safe

func (*Node) Len

func (self *Node) Len() (int, error)

Len returns children count of a array|object|string node WARN: For partially loaded node, it also works but only counts the parsed children

func (*Node) Load

func (self *Node) Load() error

Load loads the node's children as parsed. and ensure all its children can be READ concurrently (include its children's children)

func (*Node) LoadAll

func (self *Node) LoadAll() error

LoadAll loads the node's children and ensure all its children can be READ concurrently (include its children's children)

func (*Node) Map

func (self *Node) Map() (map[string]interface{}, error)

Map loads all keys of an object node

func (*Node) MapUseNode

func (self *Node) MapUseNode() (map[string]Node, error)

MapUseNode scans both parsed and non-parsed chidren nodes, and map them by their keys

func (*Node) MapUseNumber

func (self *Node) MapUseNumber() (map[string]interface{}, error)

MapUseNumber loads all keys of an object node, with numeric nodes casted to json.Number

func (*Node) MarshalJSON

func (self *Node) MarshalJSON() ([]byte, error)

func (*Node) Move added in v1.11.0

func (self *Node) Move(dst, src int) error

Move moves the child at src index to dst index, meanwhile slides sliblings from src+1 to dst.

WARN: this will change address of elements, which is a dangerous action.

func (*Node) Number

func (self *Node) Number() (json.Number, error)

Number casts node to float64, including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, V_NONE it will return error

func (*Node) Pop added in v1.11.0

func (self *Node) Pop() error

Pop remove the last child of the V_Array or V_Object node.

func (*Node) Properties

func (self *Node) Properties() (ObjectIterator, error)

Properties returns iterator for object's children traversal

func (*Node) Raw

func (self *Node) Raw() (string, error)

Raw returns json representation of the node,

func (*Node) Set

func (self *Node) Set(key string, node Node) (bool, error)

Set sets the node of given key under self, and reports if the key has existed.

If self is V_NONE or V_NULL, it becomes V_OBJECT and sets the node at the key.

func (*Node) SetAny

func (self *Node) SetAny(key string, val interface{}) (bool, error)

SetAny wraps val with V_ANY node, and Set() the node.

func (*Node) SetAnyByIndex

func (self *Node) SetAnyByIndex(index int, val interface{}) (bool, error)

SetAny wraps val with V_ANY node, and SetByIndex() the node.

func (*Node) SetByIndex

func (self *Node) SetByIndex(index int, node Node) (bool, error)

SetByIndex sets the node of given index, and reports if the key has existed.

The index must be within self's children.

func (*Node) SortKeys

func (self *Node) SortKeys(recurse bool) error

SortKeys sorts children of a V_OBJECT node in ascending key-order. If recurse is true, it recursively sorts children's children as long as a V_OBJECT node is found.

func (*Node) StrictFloat64 added in v1.3.2

func (self *Node) StrictFloat64() (float64, error)

Float64 exports underlying float64 value, includeing V_NUMBER, V_ANY

func (*Node) StrictInt64 added in v1.3.2

func (self *Node) StrictInt64() (int64, error)

StrictInt64 exports underlying int64 value, including V_NUMBER, V_ANY

func (*Node) StrictNumber added in v1.3.2

func (self *Node) StrictNumber() (json.Number, error)

Number exports underlying float64 value, including V_NUMBER, V_ANY of json.Number

func (*Node) StrictString added in v1.3.2

func (self *Node) StrictString() (string, error)

StrictString returns string value (unescaped), includeing V_STRING, V_ANY of string. In other cases, it will return empty string.

func (*Node) String

func (self *Node) String() (string, error)

String cast node to string, including V_NUMBER|V_TRUE|V_FALSE|V_ANY|V_STRING|V_NULL, V_NONE it will return error

func (Node) Type deprecated

func (self Node) Type() int

Type returns json type represented by the node It will be one of belows:

V_NONE   = 0 (empty node, key not exists)
V_ERROR  = 1 (error node)
V_NULL   = 2 (json value `null`, key exists)
V_TRUE   = 3 (json value `true`)
V_FALSE  = 4 (json value `false`)
V_ARRAY  = 5 (json value array)
V_OBJECT = 6 (json value object)
V_STRING = 7 (json value string)
V_NUMBER = 33 (json value number )
V_ANY    = 34 (golang interface{})

Deprecated: not concurrent safe. Use TypeSafe instead

func (*Node) TypeSafe added in v1.12.0

func (self *Node) TypeSafe() int

Type concurrently-safe returns json type represented by the node It will be one of belows:

V_NONE   = 0 (empty node, key not exists)
V_ERROR  = 1 (error node)
V_NULL   = 2 (json value `null`, key exists)
V_TRUE   = 3 (json value `true`)
V_FALSE  = 4 (json value `false`)
V_ARRAY  = 5 (json value array)
V_OBJECT = 6 (json value object)
V_STRING = 7 (json value string)
V_NUMBER = 33 (json value number )
V_ANY    = 34 (golang interface{})

func (*Node) UnmarshalJSON

func (self *Node) UnmarshalJSON(data []byte) (err error)

UnmarshalJSON is just an adapter to json.Unmarshaler. If you want better performance, use Searcher.GetByPath() directly

func (*Node) Unset

func (self *Node) Unset(key string) (bool, error)

Unset REMOVE (soft) the node of given key under object parent, and reports if the key has existed.

func (*Node) UnsetByIndex

func (self *Node) UnsetByIndex(index int) (bool, error)

UnsetByIndex REOMVE (softly) the node of given index.

WARN: this will change address of elements, which is a dangerous action. Use Unset() for object or Pop() for array instead.

func (*Node) Valid

func (self *Node) Valid() bool

Valid reports if self is NOT V_ERROR or nil

func (*Node) Values

func (self *Node) Values() (ListIterator, error)

Values returns iterator for array's children traversal

type ObjectIterator

type ObjectIterator struct {
	Iterator
}

ObjectIterator is specialized iterator for V_ARRAY

func (*ObjectIterator) Next

func (self *ObjectIterator) Next(p *Pair) bool

Next scans through children of underlying V_OBJECT, copies each child to v, and returns .HasNext().

type Pair

type Pair struct {
	Key   string
	Value Node
	// contains filtered or unexported fields
}

func NewPair added in v1.12.0

func NewPair(key string, val Node) Pair

type Parser

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

func NewParser

func NewParser(src string) *Parser

NewParser returns pointer of new allocated parser

func NewParserObj added in v1.10.0

func NewParserObj(src string) Parser

NewParser returns new allocated parser

func (*Parser) ExportError

func (self *Parser) ExportError(err types.ParsingError) error

ExportError converts types.ParsingError to std Error

func (*Parser) Parse

func (self *Parser) Parse() (Node, types.ParsingError)

Parse returns a ast.Node representing the parser's JSON. NOTICE: the specific parsing lazy dependens parser's option It only parse first layer and first child for Object or Array be default

func (*Parser) Pos

func (self *Parser) Pos() int

type Scanner

type Scanner func(path Sequence, node *Node) bool

type SearchOptions added in v1.12.0

type SearchOptions struct {
	// ValidateJSON indicates the searcher to validate the entire JSON
	ValidateJSON bool

	// CopyReturn indicates the searcher to copy the result JSON instead of refer from the input
	// This can help to reduce memory usage if you cache the results
	CopyReturn bool

	// ConcurrentRead indicates the searcher to return a concurrently-READ-safe node,
	// including: GetByPath/Get/Index/GetOrIndex/Int64/Bool/Float64/String/Number/Interface/Array/Map/Raw/MarshalJSON
	ConcurrentRead bool
}

SearchOptions controls Searcher's behavior

type Searcher

type Searcher struct {
	SearchOptions
	// contains filtered or unexported fields
}

func NewSearcher

func NewSearcher(str string) *Searcher

func (*Searcher) GetByPath

func (self *Searcher) GetByPath(path ...interface{}) (Node, error)

GetByPathNoCopy search in depth from top json and returns a **Referenced** json node at the path location

WARN: this search directly refer partial json from top json, which has faster speed, may consumes more memory.

func (*Searcher) GetByPathCopy added in v1.11.0

func (self *Searcher) GetByPathCopy(path ...interface{}) (Node, error)

GetByPathCopy search in depth from top json and returns a **Copied** json node at the path location

type Sequence

type Sequence struct {
	Index int
	Key   *string
}

Sequence represents scanning path of single-layer nodes. Index indicates the value's order in both V_ARRAY and V_OBJECT json. Key is the value's key (for V_OBJECT json only, otherwise it will be nil).

func (Sequence) String

func (s Sequence) String() string

String is string representation of one Sequence

type SyntaxError added in v1.6.1

type SyntaxError struct {
	Pos  int
	Src  string
	Code types.ParsingError
	Msg  string
}

func (SyntaxError) Description added in v1.6.1

func (self SyntaxError) Description() string

func (SyntaxError) Error added in v1.6.1

func (self SyntaxError) Error() string

func (SyntaxError) Message added in v1.6.1

func (self SyntaxError) Message() string

type Visitor added in v1.10.0

type Visitor interface {

	// OnNull handles a JSON null value.
	OnNull() error

	// OnBool handles a JSON true / false value.
	OnBool(v bool) error

	// OnString handles a JSON string value.
	OnString(v string) error

	// OnInt64 handles a JSON number value with int64 type.
	OnInt64(v int64, n json.Number) error

	// OnFloat64 handles a JSON number value with float64 type.
	OnFloat64(v float64, n json.Number) error

	// OnObjectBegin handles the beginning of a JSON object value with a
	// suggested capacity that can be used to make your custom object container.
	//
	// After this point the visitor will receive a sequence of callbacks like
	// [string, value, string, value, ......, ObjectEnd].
	//
	// Note:
	// 1. This is a recursive definition which means the value can
	// also be a JSON object / array described by a sequence of callbacks.
	// 2. The suggested capacity will be 0 if current object is empty.
	// 3. Currently sonic use a fixed capacity for non-empty object (keep in
	// sync with ast.Node) which might not be very suitable. This may be
	// improved in future version.
	OnObjectBegin(capacity int) error

	// OnObjectKey handles a JSON object key string in member.
	OnObjectKey(key string) error

	// OnObjectEnd handles the ending of a JSON object value.
	OnObjectEnd() error

	// OnArrayBegin handles the beginning of a JSON array value with a
	// suggested capacity that can be used to make your custom array container.
	//
	// After this point the visitor will receive a sequence of callbacks like
	// [value, value, value, ......, ArrayEnd].
	//
	// Note:
	// 1. This is a recursive definition which means the value can
	// also be a JSON object / array described by a sequence of callbacks.
	// 2. The suggested capacity will be 0 if current array is empty.
	// 3. Currently sonic use a fixed capacity for non-empty array (keep in
	// sync with ast.Node) which might not be very suitable. This may be
	// improved in future version.
	OnArrayBegin(capacity int) error

	// OnArrayEnd handles the ending of a JSON array value.
	OnArrayEnd() error
}

Visitor handles the callbacks during preorder traversal of a JSON AST.

According to the JSON RFC8259, a JSON AST can be defined by the following rules without separator / whitespace tokens.

JSON-AST  = value
value     = false / null / true / object / array / number / string
object    = begin-object [ member *( member ) ] end-object
member    = string value
array     = begin-array [ value *( value ) ] end-array

type VisitorOptions added in v1.10.0

type VisitorOptions struct {
	// OnlyNumber indicates parser to directly return number value without
	// conversion, then the first argument of OnInt64 / OnFloat64 will always
	// be zero.
	OnlyNumber bool
}

VisitorOptions contains all Visitor's options. The default value is an empty VisitorOptions{}.

Jump to

Keyboard shortcuts

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