Documentation
¶
Overview ¶
Package ast defines an abstract syntax tree for JSON values, and a parser that constructs syntax trees from JSON source.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ErrEmptyInput = errors.New("empty input")
ErrEmptyInput is a sentinel error reported by Parse if the input is empty.
var ErrExtraInput = errors.New("extra data after value")
ErrExtraInput is a sentinel error reported by ParseOne if the input contains additional values after the first one.
var Null nullValue
Null represents the JSON null constant. The length of Null is defined as 0.
Functions ¶
func ParseRange ¶ added in v0.1.1
ParseRange parses and yields the JSON values from r. Each pair produced by the iterator is either a valid JSON value and nil error, or a nil value and a non-nil parse error. If and when an error occurs, the iterator stops. If the input is empty, the iterator yields no values.
func TextEqual ¶
TextEqual returns a matching function for FindKey and IndexKey that reports whether its argument is case-sensitively equal to key.
func TextEqualFold ¶
TextEqualFold returns a matching function for FindKey and IndexKey that reports whether its argument is case-insensitively equal to key.
Types ¶
type Array ¶
type Array []Value
An Array is a sequence of values.
type Float ¶
type Float float64
A Float is represents a floating-point number.
type Int ¶
type Int int64
An Int represents an integer number.
type Member ¶
A Member is a single key-value pair belonging to an Object. Although a member is not technically a value in the JSON grammar, this type implements the Value interface for convenience of use.
type Number ¶
type Number interface {
Value
IsInt() bool // reports whether the value is an integer
Int() Int // converts the value to an integer
Float() Float // converts the value to floating point
}
A Number is a Value that represents a number. Number values have the property that they can be converted into Int or Float.
type Object ¶
type Object []*Member
An Object is a collection of key-value members.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
A Parser parses and returns JSON values from a reader.
func (*Parser) AllowJWCC ¶
AllowJWCC configures p to accept (true) or reject (false) JWCC extensions supporting comments and trailing commas. Setting this option to true does not include comments in the result; it only instructs the parser to accept input that includes comments and trailing commas.
See: https://nigeltao.github.io/blog/2021/json-with-commas-comments.html
type Text ¶
type Text struct {
// contains filtered or unexported fields
}
Text represents a string value. Text values may either be "quoted", as represented in JSON source text, or "unquoted" (with quotation marks and escape sequences removed).
The String method returns the plain (unescaped) text. The JSON method returns the quoted (escaped) JSON text.
func Quoted ¶
Quoted constructs a Text value representing the quoted value s. It is the caller's responsibility to ensure that s is properly quoted.
func (Text) Quote ¶
Quote returns the quoted representation of t, which is t itself if the text was already quoted.
func (Text) Spelling ¶ added in v0.1.5
Spelling returns the spelling of t as-written, which includes quotes and escape sequences if t is quoted.
type Value ¶
type Value interface {
// JSON converts the value into JSON source text.
JSON() string
// String converts the value into a human-readable string. The result is
// not required to be valid JSON.
String() string
}
A Value is an arbitrary JSON value.
func AnchorValue ¶
AnchorValue constructs a Value from the specified anchor, or reports an error if the anchor does not record a value.
func Parse ¶
Parse parses and returns the JSON values from r. In case of error, any complete values already parsed are returned along with the error. It reports ErrEmptyInput if the input is entirely empty.
func ParseSingle ¶
ParseSingle parses and returns a single JSON value from r. If r contains no values, ParseSingle reports nil, ErrEmptyInput. If r contains more data after the first value, ParseSingle returns the first value, and ErrExtraInput.