token

package
v0.0.0-...-655fa59 Latest Latest
Warning

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

Go to latest
Published: Jul 26, 2026 License: Apache-2.0 Imports: 6 Imported by: 1

Documentation

Overview

Package token defines the JSON token types with their kind.

These data structures should be returned by all implementations of a JSON lexer.

There are two flavors of supported tokens:

- the basic token type T, which is suitable for most purposes

The verbatim token type should be reserved to specific use-cases such as rendering verbatim documents (e.g. for linters, auto-fixers, editor plugins, etc.)

Index

Constants

This section is empty.

Variables

View Source
var EOFToken = T{
	// contains filtered or unexported fields
}

EOFToken is a preallocated placeholder returned whenever the lexer has reached the end of the input stream.

View Source
var None = T{
	// contains filtered or unexported fields
}

None is a preallocated placeholder for any invalid or unrecognized JSON token.

View Source
var NullToken = T{
	// contains filtered or unexported fields
}

NullToken is a preallocated placeholder for "null" tokens.

Functions

func Unescape

func Unescape(raw []byte) []byte

Unescape returns the decoded form of a raw string/Key value produced by the verbatim lexer [lexer.VL].

VL keeps string/Key values exactly as they appeared in the source (escape sequences intact — see the String/Key doc), so the token stream can be round-tripped byte-for-byte. Unescape expands the JSON escapes on demand: the shorthand escapes (\", \\, \/, \b, \f, \n, \r, \t) and \uXXXX sequences (surrogate pairs combined) become their UTF-8 bytes.

If raw contains no escape it is returned unchanged with no allocation; otherwise a fresh slice is returned. Do NOT call this on a semantic-lexer value — the semantic lexer [lexer.L] already decodes.

The escapes were validated when the token was scanned, so decoding cannot fail; a malformed sequence would have errored at scan time (any residual bad input is passed through rather than panicking).

func UnescapeString

func UnescapeString(raw []byte) string

UnescapeString is Unescape as a string. It always allocates (the string header cannot alias the token's buffer, which is reused on the next token).

Types

type Kind

type Kind uint8

Kind of JSON token, i.e. either a delimiter, a string, a number, a boolean or null.

EOF is considered a special token that marks the end of a JSON stream.

Strings and numbers are not converted to go string and go numeric types respectively: the original value is kept as a slice of bytes.

const (
	// Unknown token.
	//
	// This result is associated with an error in the lexer.
	Unknown Kind = iota

	// Delimiter token, i.e. ",", ":", "{", "}", "[", "]".
	Delimiter

	// String token.
	//
	// From the semantic lexer [lexer.L], [T.Value] is unescaped (decoded); from the
	// verbatim lexer [lexer.VL] it keeps the raw source bytes (escapes intact) for
	// faithful round-tripping — decode on demand with [Unescape].
	String

	// Key string token.
	//
	// Like [String]: semantic value is decoded, verbatim value is raw (see [Unescape]).
	Key

	// Number JSON token.
	Number

	// Boolean token.
	Boolean

	// Null value token.
	Null

	// EOF signals that the lexer has reached the end of the input stream.
	EOF
)

JSON tokens.

func (Kind) String

func (k Kind) String() string

String representation of a kind of token.

type KindDelimiter

type KindDelimiter uint8

KindDelimiter represents a JSON delimiter (i.e. ":", ",", "{", "}", "[", "]").

const (
	// NotADelimiter is the zero value, used when the token is not a delimiter.
	NotADelimiter KindDelimiter = iota

	// Comma is ","
	Comma

	// Colon is ":"
	Colon

	// OpeningBracket is "{"
	OpeningBracket

	// ClosingBracket is "}"
	ClosingBracket

	// OpeningSquareBracket is "["
	OpeningSquareBracket

	// ClosingSquareBracket is "]"
	ClosingSquareBracket
)

Delimiters.

func (KindDelimiter) AcceptValue

func (d KindDelimiter) AcceptValue() bool

AcceptValue returns true when the delimiter may be followed by a value token.

Examples: ": true", "[\"abc\"]", ",123", {"abc" are legit but not: "} true", "] 123"

Notice that {123 or {true are accepted: more context is needed to reject such constructs.

func (KindDelimiter) IsClosing

func (d KindDelimiter) IsClosing() bool

IsClosing returns true for closing delimiters such as "}" or "]"

func (KindDelimiter) String

func (d KindDelimiter) String() string

String representation of a delimiter.

type T

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

T represents a JSON token.

Tokens are immutable, short-lived objects.

T maintains strings and numbers as slices of bytes representing an UTF8 string.

It doesn't keep track of non-significant blank space or new lines.

Escaped unicode sequences are unescaped as UTF8 runes.

Limitation: JSON data based on a non-UTF8 character set need to be converted beforehand.

func Make

func Make(kind Kind, value []byte, delimiter KindDelimiter, valueBool bool) T

Make a token T.

func MakeBoolean

func MakeBoolean(value bool) T

MakeBoolean builds a scalar boolean token T.

func MakeDelimiter

func MakeDelimiter(delimiter KindDelimiter) T

MakeDelimiter builds a delimiter token T.

func MakeWithValue

func MakeWithValue(kind Kind, value []byte) T

MakeWithValue builds a scalar string or number token T.

func (T) Bool

func (t T) Bool() bool

Bool value for boolean tokens.

func (T) Clone

func (t T) Clone() T

Clone deep-clones a token.

Memory to hold the token's string or numeric value will be freshly allocated.

func (T) Delimiter

func (t T) Delimiter() KindDelimiter

Delimiter for delimiter tokens.

The value is NotADelimiter for non-delimiter tokens.

func (T) IsBool

func (t T) IsBool() bool

IsNull indicates if the Token represents a boolean value.

func (T) IsColon

func (t T) IsColon() bool

func (T) IsComma

func (t T) IsComma() bool

func (T) IsDelimiter

func (t T) IsDelimiter() bool

func (T) IsEOF

func (t T) IsEOF() bool

func (T) IsEndArray

func (t T) IsEndArray() bool

func (T) IsEndObject

func (t T) IsEndObject() bool

func (T) IsKey

func (t T) IsKey() bool

func (T) IsKnown

func (t T) IsKnown() bool

IsKnown checks if the token is valid.

func (T) IsNull

func (t T) IsNull() bool

IsNull indicates if the Token represents the null value.

func (T) IsScalar

func (t T) IsScalar() bool

IsScalar indicates is the Token represents a scalar value (null is not considered a scalar).

func (T) IsStartArray

func (t T) IsStartArray() bool

func (T) IsStartObject

func (t T) IsStartObject() bool

func (T) Kind

func (t T) Kind() Kind

Kind of token.

func (T) String

func (t T) String() string

String representation of a token.

This is intended for logging or debug mostly.

func (T) Value

func (t T) Value() []byte

Value for String, Key and Number tokens.

For a token produced by the verbatim lexer [lexer.VL], string/Key values are RAW (escapes intact); decode on demand with Unescape. The semantic lexer [lexer.L] returns already-decoded values.

Jump to

Keyboard shortcuts

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