ast

package
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Feb 21, 2026 License: BSD-3-Clause Imports: 10 Imported by: 1

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

View Source
var ErrEmptyInput = errors.New("empty input")

ErrEmptyInput is a sentinel error reported by Parse if the input is empty.

View Source
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.

View Source
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

func ParseRange(r io.Reader) iter.Seq2[Value, error]

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

func TextEqual(key string) func(Text) bool

TextEqual returns a matching function for FindKey and IndexKey that reports whether its argument is case-sensitively equal to key.

func TextEqualFold

func TextEqualFold(key string) func(Text) bool

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.

func ArrayOf added in v0.1.6

func ArrayOf[T any](args ...T) Array

ArrayOf constructs an array from args by invoking ToValue on each argument.

func (Array) JSON

func (a Array) JSON() string

JSON renders the array as JSON text.

func (Array) Len

func (a Array) Len() int

Len returns the number of elements in a.

func (Array) String

func (a Array) String() string

type Bool

type Bool bool

A Bool is a Boolean constant, true or false.

func (Bool) JSON

func (b Bool) JSON() string

JSON returns b as JSON text.

func (Bool) String

func (b Bool) String() string

type Float

type Float float64

A Float is represents a floating-point number.

func (Float) Float

func (f Float) Float() Float

Float satisfies the Numeric interface. It returns f unmodified.

func (Float) Int

func (f Float) Int() Int

Int satisfies the numeric interface.

func (Float) IsInt

func (Float) IsInt() bool

IsInt reports false for f.

func (Float) JSON

func (f Float) JSON() string

JSON renders f as JSON text.

func (Float) String

func (f Float) String() string

type Int

type Int int64

An Int represents an integer number.

func (Int) Float

func (z Int) Float() Float

Float satisfies the Numeric interface.

func (Int) Int

func (z Int) Int() Int

Int satisfies the Numeric interface. It returns z unmodified.

func (Int) IsInt

func (Int) IsInt() bool

IsInt reports true for z.

func (Int) JSON

func (z Int) JSON() string

JSON renders z as JSON text.

func (Int) String

func (z Int) String() string

type Member

type Member struct {
	Key   Text
	Value Value
}

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.

func Field

func Field(key string, value any) *Member

Field constructs an object member with the given key and value. The value must be a string, int, float, bool, nil, or ast.Value.

func (Member) JSON

func (m Member) JSON() string

JSON renders the member as JSON text.

func (Member) String

func (m Member) String() string

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.

func (Object) Find

func (o Object) Find(key string) *Member

Find is shorthand for FindKey with a case-insensitive name match on key.

func (Object) FindKey

func (o Object) FindKey(f func(Text) bool) *Member

FindKey returns the first member of o for whose key f reports true, or nil.

func (Object) IndexKey

func (o Object) IndexKey(f func(Text) bool) int

IndexKey returns the index of the first member of o for whose key f reports true, or -1.

func (Object) JSON

func (o Object) JSON() string

JSON renders o as JSON text.

func (Object) Len

func (o Object) Len() int

Len returns the number of members in the object.

func (Object) Sort

func (o Object) Sort()

Sort sorts the object in ascending order by key.

func (Object) String

func (o Object) String() string

type Parser

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

A Parser parses and returns JSON values from a reader.

func NewParser

func NewParser(r io.Reader) *Parser

NewParser constructs a parser that consumes input from r.

func (*Parser) AllowJWCC

func (p *Parser) AllowJWCC(ok bool)

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

func (*Parser) Parse

func (p *Parser) Parse() (Value, error)

Parse parses and returns the next JSON value from its input. It returns io.EOF if no further values are available.

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

func Quoted(s string) Text

Quoted constructs a Text value representing the quoted value s. It is the caller's responsibility to ensure that s is properly quoted.

func String

func String(s string) Text

String constructs a Text value representing the unquoted value s.

func (Text) JSON added in v0.1.5

func (t Text) JSON() string

JSON returns the JSON encoding of q.

func (Text) Len added in v0.1.5

func (t Text) Len() int

Len returns the length in bytes of the unquoted text of t.

func (Text) Quote

func (t Text) Quote() Text

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

func (t Text) Spelling() string

Spelling returns the spelling of t as-written, which includes quotes and escape sequences if t is quoted.

func (Text) String added in v0.1.5

func (t Text) String() string

String returns the unquoted string represented by t.

func (Text) Unquote added in v0.1.5

func (t Text) Unquote() Text

Unquote returns the unquoted representation of t, which is t itself if the text was already unquoted.

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

func AnchorValue(loc jtree.Anchor) (Value, error)

AnchorValue constructs a Value from the specified anchor, or reports an error if the anchor does not record a value.

func Parse

func Parse(r io.Reader) ([]Value, error)

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

func ParseSingle(r io.Reader) (Value, error)

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.

func ToValue

func ToValue(v any) Value

ToValue converts a string, int, float, bool, nil, or ast.Value into an ast.Value. It panics if v does not have one of those types.

Jump to

Keyboard shortcuts

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