Skip to content

Repository files navigation

@jsxtools/jsonc-reader

Tokenize JSONC and parse it into a compact span-based AST.

Install

npm install @jsxtools/jsonc-reader

Usage

import{JsonAstKind,TokenKind,parse,tokenize}from"@jsxtools/jsonc-reader";constsource='{\n // comment\n "answer": [42, true, null],\n}';consttokens=tokenize(source);constast=parse(tokens);if(ast.t===JsonAstKind.Object){console.log(ast.m[0]?.k.t===TokenKind.String);}

What this package does

  • 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.

What this package does not do

  • 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.

API

tokenize(input, options?)

Returns an array of tokens plus a non-enumerable text property containing the original source text.

  • input: any value, coerced with String(input)
  • options.allowComments: defaults to true

parse(tokens)

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.

JsoncSyntaxError

Syntax errors include:

  • reason
  • s start offset
  • line
  • column

Development

  • npm run build
  • npm run test
  • npm run lint
  • npm run format:check

About

Tokenize JSONC and parse it into a compact span-based AST.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages