Documentation
¶
Overview ¶
Package jsonvx is a highly configurable JSON parser, querier, and formatter for Go.
It supports both strict JSON (as defined by ECMA-404) and up to a more relaxed variant such as JSON5. This makes it ideal for parsing user-generated data, configuration files, or legacy formats that don't fully comply with the JSON specification.
With a single ParserConfig struct, jsonvx gives you fine-grained control over how JSON is parsed. You can enable or disable features like:
- Hexadecimal numbers (0xFF)
- NaN and Infinity as numeric values
- Leading plus signs in numbers (+42)
- Decimal edge cases like .5 or 5.
- Unquoted object keys ({key: "value"})
- Single-quoted strings ('text')
- Newlines inside strings
- Escape characters outside the standard set
- Trailing commas in arrays or objects
- Line comments (// comment) and Block comments (/* comment */)
- Extra whitespace in unusual places
- Enable JSON5
After parsing, jsonvx gives you a clean abstract syntax tree (AST) that you can either traverse manually or query using the built-in API. Each node in the tree implements a common JSON interface, so you can safely inspect, transform, or stringify data as needed.
jsonvx is designed for flexibility and correctness — not raw performance. It prioritizes clarity and configurability over speed, making it perfect for tools, linters, formatters, and config loaders where input may vary or include non-standard extensions.
If you need full control over how JSON is interpreted and a structured way to work with the result, jsonvx is for you.
Index ¶
- Variables
- func ToFloat(input []byte) (float64, error)
- func ToInt(input []byte) (int64, error)
- func WithAllowBlockComments(allow bool) func(*ParserConfig)
- func WithAllowExtraWS(allow bool) func(*ParserConfig)
- func WithAllowHexNumbers(allow bool) func(*ParserConfig)
- func WithAllowInfinity(allow bool) func(*ParserConfig)
- func WithAllowLeadingPlus(allow bool) func(*ParserConfig)
- func WithAllowLineComments(allow bool) func(*ParserConfig)
- func WithAllowNaN(allow bool) func(*ParserConfig)
- func WithAllowNewlineInStrings(allow bool) func(*ParserConfig)
- func WithAllowOtherEscapeChars(allow bool) func(*ParserConfig)
- func WithAllowPointEdgeNumbers(allow bool) func(*ParserConfig)
- func WithAllowSingleQuotes(allow bool) func(*ParserConfig)
- func WithAllowTrailingCommaArray(allow bool) func(*ParserConfig)
- func WithAllowTrailingCommaObject(allow bool) func(*ParserConfig)
- func WithAllowUnquoted(allow bool) func(*ParserConfig)
- func WrapJSONMultipleContentError(token Token) error
- func WrapJSONSyntaxError(token Token) error
- func WrapJSONUnexpectedCharError(token Token) error
- func WrapUnexpectedCharError(baseErr error, token Token) error
- type Array
- type ArrayCallback
- type Boolean
- type JSON
- type KeyValue
- type Lexer
- type Null
- type Number
- type Object
- type ObjectCallback
- type Parser
- type ParserConfig
- type String
- type Token
- type TokenKind
- type TokenSubKind
- type Tokens
Constants ¶
This section is empty.
Variables ¶
var ( ErrNotString = errors.New("value is not a JSON string") ErrNotNumber = errors.New("value is not a JSON number") ErrNotBoolean = errors.New("value is not a JSON boolean") ErrNotNull = errors.New("value is not null") ErrInvalidQueryKey = errors.New("invalid query key") ErrExpectedIndex = errors.New("invalid query key, expected integer index") ErrIndexOutOfRange = errors.New("index out of range") ErrEmptyArray = errors.New("array is empty") ErrInvalidJSONType = errors.New("invalid JSON type") ErrQueryExceedsDepth = errors.New("query exceeds depth for scalar value") )
Common errors for type assertion and query path resolution.
Functions ¶
func WithAllowBlockComments ¶
func WithAllowBlockComments(allow bool) func(*ParserConfig)
func WithAllowExtraWS ¶
func WithAllowExtraWS(allow bool) func(*ParserConfig)
WithAllowExtraWS is the functional option setters for the AllowExtraWS flag.
func WithAllowHexNumbers ¶
func WithAllowHexNumbers(allow bool) func(*ParserConfig)
Functional option setters for the AllowHexNumbers flag.
func WithAllowInfinity ¶
func WithAllowInfinity(allow bool) func(*ParserConfig)
func WithAllowLeadingPlus ¶
func WithAllowLeadingPlus(allow bool) func(*ParserConfig)
func WithAllowLineComments ¶
func WithAllowLineComments(allow bool) func(*ParserConfig)
func WithAllowNaN ¶
func WithAllowNaN(allow bool) func(*ParserConfig)
func WithAllowNewlineInStrings ¶
func WithAllowNewlineInStrings(allow bool) func(*ParserConfig)
func WithAllowOtherEscapeChars ¶
func WithAllowOtherEscapeChars(allow bool) func(*ParserConfig)
func WithAllowPointEdgeNumbers ¶
func WithAllowPointEdgeNumbers(allow bool) func(*ParserConfig)
Functional option setters for the AllowHexNumbers flag.
func WithAllowSingleQuotes ¶
func WithAllowSingleQuotes(allow bool) func(*ParserConfig)
func WithAllowTrailingCommaArray ¶
func WithAllowTrailingCommaArray(allow bool) func(*ParserConfig)
func WithAllowTrailingCommaObject ¶
func WithAllowTrailingCommaObject(allow bool) func(*ParserConfig)
func WithAllowUnquoted ¶
func WithAllowUnquoted(allow bool) func(*ParserConfig)
func WrapJSONSyntaxError ¶
func WrapUnexpectedCharError ¶
Types ¶
type Array ¶
type Array struct {
Items []JSON
}
Array represents a JSON array.
func (*Array) ForEach ¶
func (a *Array) ForEach(cb ArrayCallback)
ForEach calls the given callback for each item in the array. It provides the item, its index, and the array itself.
type ArrayCallback ¶
ArrayCallback defines the function signature for iterating over items in a JSON array. - item: the current element - index: the index of the element - array: the array being iterated
type Boolean ¶
type Boolean struct {
Token *Token
}
Boolean represents a JSON boolean (true or false).
type KeyValue ¶
type KeyValue struct {
// contains filtered or unexported fields
}
KeyValue represents a key-value pair in a JSON object.
type Lexer ¶
type Lexer struct {
// contains filtered or unexported fields
}
Lexer performs lexical analysis on a JSON-like input stream. It breaks the raw input into a sequence of tokens according to the parsing rules defined in the provided ParserConfig.
The Lexer operates on raw byte slices for performance, and tracks position and line information to support error reporting and debugging.
func NewLexer ¶
func NewLexer(input []byte, cfg *ParserConfig) *Lexer
NewLexer creates a new Lexer instance using the given input and configuration.
It immediately reads the first character to initialize internal state, so the lexer is ready for tokenization right after creation.
func (*Lexer) String ¶
String returns a human-readable representation of the current state of the Lexer. It includes the input, current line number, character position, read position, and the current character.
func (*Lexer) TokensWithout ¶
TokensWithoutWhitespace returns a slice of all tokens without whitespace produced so far by the lexer.
type Number ¶
type Number struct {
Token *Token
}
Number represents a JSON number (integer, float, hex, etc.).
type Object ¶
type Object struct {
Properties []KeyValue
}
Object represents a JSON object.
func (*Object) ForEach ¶
func (o *Object) ForEach(cb ObjectCallback)
ForEach calls the given callback for each key-value pair in the object. It provides the key, value, and the object itself.
type ObjectCallback ¶
ObjectCallback defines the function signature for iterating over properties in a JSON object. - key: the property's key as a byte slice - value: the property's value - object: the object being iterated
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
func NewParser ¶
func NewParser(input []byte, config *ParserConfig) Parser
type ParserConfig ¶
type ParserConfig struct {
AllowExtraWS bool // AllowExtraWS allows extra whitespace characters that are not normally permitted by strict JSON.
AllowHexNumbers bool // AllowHexNumbers enables support for hexadecimal numeric literals (e.g., 0xFF).
AllowPointEdgeNumbers bool // AllowPointEdgeNumbers allows numbers like `.5` or `5.` without requiring a digit before/after the decimal point.
AllowInfinity bool // AllowInfinity enables the use of `Infinity` and `-Infinity` as number values.
AllowNaN bool // AllowNaN allows `NaN` (Not-a-Number) as a numeric value.
AllowLeadingPlus bool // AllowLeadingPlus permits a leading '+' in numbers (e.g., `+42`).
AllowUnquoted bool // AllowUnquoted enables parsing of unquoted object keys (e.g., `{foo: "bar"}`).
AllowSingleQuotes bool // AllowSingleQuotes allows strings to be enclosed in single quotes (' ') in addition to double quotes.
AllowNewlineInStrings bool // AllowNewlineInStrings permits multiple new line characters being escaped.
AllowOtherEscapeChars bool // AllowOtherEscapeChars enables support for escape sequences other than \\, \/, \b, \n, \f, \r, \t and Unicode escapes (\uXXXX).
AllowTrailingCommaArray bool // AllowTrailingCommaArray permits a trailing comma in array literals (e.g., `[1, 2, ]`).
AllowTrailingCommaObject bool // AllowTrailingCommaObject permits a trailing comma in object literals (e.g., `{"a": 1,}`).
AllowLineComments bool // AllowLineComments enables the use of single-line comments (// ...).
AllowBlockComments bool // AllowBlockComments enables the use of block comments (/* ... */).
}
ParserConfig defines the encoding and decoding behavior for the JSON parser.
By default, all fields are false (strict mode). Enabling individual fields allows the parser to accept features that are not allowed by the standard ECMA-404 specification but may appear in relaxed formats like JSON5 or user-generated JSON.
func JSON5Config ¶
func JSON5Config() *ParserConfig
JSON5Config returns a ParserConfig with all features enabled for JSON5 compatibility.
func NewParserConfig ¶
func NewParserConfig(opts ...func(*ParserConfig)) *ParserConfig
NewParserConfig creates a new ParserConfig instance, optionally applying one or more configuration options. Options are applied in the order provided.
func (*ParserConfig) String ¶
func (c *ParserConfig) String() string
String returns a formatted string representing all configuration options in the ParserConfig. Each field is listed with its corresponding boolean value.
type Token ¶
type Token struct {
Kind TokenKind // The general kind of the token (e.g., STRING, NUMBER).
SubKind TokenSubKind // The specific sub kind within a kind (e.g., INTEGER vs FLOAT).
Literal []byte // The literal value of the token.
Line int // The line number where the token appears (1-based index).
Column int // The column position in the line (0-based index).
}
Token represents a single lexical token, including its kind, value, and position.
type TokenKind ¶
type TokenKind int
TokenKind represents the kind of a token, used during both lexical analysis and by tools that query parsed node structures.
const ( EOF TokenKind = iota // EOF indicates the end of input. ILLEGAL // ILLEGAL indicates an unrecognized or invalid token. WHITESPACE // WHITESPACE represents any space character. COMMENT // COMMENT represents a comment STRING // STRING represents a string literal. NUMBER // NUMBER represents any numeric literal. NULL // NULL represents a null value. BOOLEAN // BOOLEAN represents a boolean value. COMMA // COMMA represents a ',' separator. COLON // COLON represents a ':' separator. LEFT_SQUARE_BRACE // LEFT_SQUARE_BRACE represents '['. RIGHT_SQUARE_BRACE // RIGHT_SQUARE_BRACE represents ']'. LEFT_CURLY_BRACE // LEFT_CURLY_BRACE represents '{'. RIGHT_CURLY_BRACE // RIGHT_CURLY_BRACE represents '}'. )
type TokenSubKind ¶
type TokenSubKind int
TokenSubKind represents the subtype of a token, such as numeric format or identifier type.
const ( NONE TokenSubKind = iota // NONE represents the absence of a sub kind. FALSE // FALSE represents a boolean false. TRUE // TRUE represents a boolean true. SINGLE_QUOTED // SINGLE_QUOTED represents a single quoted string value. DOUBLE_QUOTED // DOUBLE_QUOTED represents a double quoted string value. IDENT // IDENT represents an unquoted identifier. INTEGER // INTEGER represents an integer (positive or negative). FLOAT // FLOAT represents a floating-point number (positive or negative). SCI_NOT // SCI_NOT represents scientific notation (e.g., 1e10). HEX // HEX represents a hexadecimal number (e.g., 0xFF). INF // INF represents an Infinity literal (positive or negative). NaN // NaN represents a Not-a-Number literal. LINE_COMMENT // LINE_COMMENT represents a single-line comment (// ...). BLOCK_COMMENT // BLOCK_COMMENT represents a block comment (/* ... */). INVALID_CHARACTER // INVALID_CHARACTER represents an invalid or unexpected character. INVALID_WHITESPACE // INVALID_WHITESPACE represents an invalid or misplaced whitespace. INVALID_NULL // INVALID_NULL represents an invalid 'null' literal. INVALID_TRUE // INVALID_TRUE represents an invalid 'true' literal. INVALID_FALSE // INVALID_FALSE represents an invalid 'false' literal. INVALID_COMMENT // INVALID_COMMENT represents an improperly formed comment. INVALID_LINE_COMMENT // INVALID_LINE_COMMENT represents a malformed single-line comment. INVALID_BLOCK_COMMENT // INVALID_BLOCK_COMMENT represents a malformed block comment. INVALID_STRING // INVALID_STRING represents a malformed or unterminated string. INVALID_HEX_STRING // INVALID_HEX_STRING represents an invalid hexadecimal string. INVALID_NEWLINE_STRING // INVALID_NEWLINE_STRING represents a string that contains an invalid newline. INVALID_ESCAPED_STRING // INVALID_ESCAPED_STRING represents a string with an invalid escape sequence. INVALID_LEADING_ZERO // INVALID_LEADING_ZERO represents a number with an invalid leading zero. INVALID_LEADING_PLUS // INVALID_LEADING_PLUS represents a number with an invalid leading plus sign. INVALID_NaN // INVALID_NaN represents a malformed NaN literal. INVALID_INF // INVALID_INF represents a malformed Infinity literal. INVALID_POINT_EDGE_DOT // INVALID_POINT_EDGE_DOT represents a number with a misplaced or standalone dot. INVALID_HEX_NUMBER // INVALID_HEX_NUMBER represents a malformed hexadecimal number. )
func (TokenSubKind) String ¶
func (t TokenSubKind) String() string
String returns a string representation of the TokenSubKind.