Skip to content

@lanexio/parser-grammar-json

This page documents @lanexio/parser-grammar-json, the JSON grammar package supporting strict JSON (RFC 8259), JSONC (comments and trailing commas), and JSON5 (ES5 relaxations).

  • Version: Stable
  • Module name: parser-grammar-json
  • Package: @lanexio/parser-grammar-json
  • Import path: @lanexio/parser-grammar-json
  • Layer: 2 (Grammar)
  • Runtime: Universal
  • Module format: ESM
  • Stability: Stable
  • Primary use case: Parse JSON, JSONC, and JSON5 documents into a flat AST.
  • You need to parse strict JSON (RFC 8259) into a traversable AST.
  • You need to parse JSONC (JSON with Comments) or JSON5 (ES5 syntax relaxations).
  • You need structured AST with object members, arrays, and value nodes.
BoundaryDescription
InputsUint8Array (source bytes) + optional ParseJsonDialectOptions
OutputsLexTree (flat AST rooted at JsonKind.Document)
Side effectsNone
DeterminismYes (same bytes + same dialect produce identical tree)
External dependencies@lanexio/parser-core
Never-throw guaranteeYes
Security surfaceNone (parse only, no output generation)
  1. Install the package.

    Terminal window
    pnpm add @lanexio/parser-grammar-json
  2. Import the named export.

    import { parseJson } from '@lanexio/parser-grammar-json';
RequirementRequiredLayerNotes
@lanexio/parser-coreYes1^1.0.0
import { parseJson } from '@lanexio/parser-grammar-json';
const encoder = new TextEncoder();
const bytes = encoder.encode('{"key": [1, true, null]}');
const tree = parseJson(bytes);
console.log(tree.nodeCount);
console.log(tree.root.kind); // JsonKind.Document
import { parseJsonc } from '@lanexio/parser-grammar-json';
const encoder = new TextEncoder();
const tree = parseJsonc(encoder.encode('/* comment */ { "key": 1 }'));
import { parseJson5 } from '@lanexio/parser-grammar-json';
const encoder = new TextEncoder();
const tree = parseJson5(encoder.encode('{ key: "value", }')); // unquoted keys, trailing comma
ExportTypeDescription
parseJson(bytes: Uint8Array) => LexTreeParse strict JSON (RFC 8259). Convenience wrapper.
parseJsonc(bytes: Uint8Array) => LexTreeParse JSONC (JSON with Comments and trailing commas).
parseJson5(bytes: Uint8Array) => LexTreeParse JSON5 (ES5 relaxations).
parseWithDialect(bytes: Uint8Array, options: ParseJsonDialectOptions) => LexTreeParse with explicit dialect selection.
STRICT_JSONJsonDialectStrict JSON dialect (RFC 8259).
JSONCJsonDialectJSONC dialect (comments, trailing commas).
JSON5JsonDialectJSON5 dialect (unquoted keys, single quotes, etc.).
JsonKindconst objectNumeric kind IDs for all JSON node types.
JsonFieldconst objectNumeric field IDs for JSON value slots.
JSON_FIELD_NAMES_BY_IDreadonly string[]Field name lookup by numeric field ID.
JSON_KIND_NAMES_BY_IDreadonly Record<number, string>Kind-name lookup by numeric ID.
jsonGrammarLanexioParserPureGrammarGrammar descriptor for strict JSON.
jsoncGrammarLanexioParserPureGrammarGrammar descriptor for JSONC.
json5GrammarLanexioParserPureGrammarGrammar descriptor for JSON5.
jsonRegistrationGrammarRegistrationRegistration for the unified grammar registry.
jsoncRegistrationGrammarRegistrationRegistration for JSONC.
json5RegistrationGrammarRegistrationRegistration for JSON5.
jsonReuseOracleReuseOracleIncremental reuse oracle for strict JSON.
jsoncReuseOracleReuseOracleIncremental reuse oracle for JSONC.
json5ReuseOracleReuseOracleIncremental reuse oracle for JSON5.
FieldTypeRequiredDefaultDescription
dialectJsonDialectYes-The JSON dialect to use (STRICT_JSON, JSONC, or JSON5).
PropertyTypeDescription
rootLexNodeRoot node (JsonKind.Document).
nodeCountnumberTotal nodes in the tree.
sourceUint8ArrayOriginal parsed bytes.
TypePurposeNotes
ParseJsonDialectOptionsOptions for parseWithDialect{ dialect: JsonDialect }
JsonDialectJSON dialect definitionDefines parsing rules
JsonKindTypeUnion of all JsonKind valuesType-safe kind reference
JsonFieldTypeUnion of all JsonField valuesType-safe field reference

Use parseWithDialect to select the parsing dialect explicitly. The convenience functions parseJson, parseJsonc, and parseJson5 are thin wrappers around parseWithDialect.

  • No direct accessibility surface. The JSON parser produces flat AST data structures. Consuming code is responsible for rendering output with appropriate semantics.
ConcernStatus
Generated output semanticsNot applicable (data format)
ARIA attributes in serialized outputNot applicable
Semantic element round-tripNot applicable
  • parseJson, parseJsonc, and parseJson5 never throw on any byte sequence.
ThreatMitigationStatus
Malformed input byte sequencePanic-free guarantee: all inputs accepted, errors produce JsonKind.Error AST nodesImplemented
PackageRelationshipLayerNotes
@lanexio/parser-coreRequires1Provides LexTree, LexNode, LexCursor
@lanexio/parserConsumes6Unified entry point
VersionDateStatusNotable changes
1.0.02026-05-29CurrentInitial stable release. Apache-2.0.
  • None.