Tokenize JSONC and parse it into a compact span-based AST.
npm install @jsxtools/jsonc-readerimport { JsonAstKind, TokenKind, parse, tokenize } from "@jsxtools/jsonc-reader";
const source = '{\n // comment\n "answer": [42, true, null],\n}';
const tokens = tokenize(source);
const ast = parse(tokens);
if (ast.t === JsonAstKind.Object) {
console.log(ast.m[0]?.k.t === TokenKind.String);
}- Tokenizes JSONC source into tokens with offsets, line numbers, and columns.
- Parses those tokens into a compact AST that preserves source spans.
- Accepts JSONC comments by default.
- Accepts trailing commas in arrays and objects.
- It does not materialize JavaScript values like
JSON.parse. - It does not preserve comments in the AST; comments remain tokens.
- It does not allow single-quoted strings.
Returns an array of tokens plus a non-enumerable text property containing the original source text.
input: any value, coerced withString(input)options.allowComments: defaults totrue
Consumes a token collection and returns a span-based AST.
Primitive nodes are represented by token kind plus start/end offsets. Object and array nodes expose members via m.
Syntax errors include:
reasonsstart offsetlinecolumn
npm run buildnpm run testnpm run lintnpm run format:check