Skip to content

StyleShit/json-parser

Repository files navigation

JSON Parser

Simple JSON to AST parser in TypeScript.

This parser has a custom grammar and exists for learning purposes.

Features

The parser supports the following JSON types:

  • null
  • boolean
  • number
  • string
  • array
  • object

In addition, it supports trailing commas in arrays and objects (for god's sake!).

Installation

npm install @styleshit/json-parser

Usage

To use it, import the parse function and pass a JSON string to it.

For example, this code:

import { parse } from '@styleshit/json-parser';

const ast = parse('{"hello": "world", "foo": 42,}');

console.log(ast);

Will output:

{
  kind: 'object',
  members: {
    hello: {
      kind: 'string',
      value: 'world',
    },
    foo: {
      kind: 'number',
      value: 42,
    },
  },
}

While this code will throw an error:

import { parse } from '@styleshit/json-parser';

const ast = parse('{"hello": "world", "foo": 42} true');

// SyntaxError: Unexpected 't' at index 30

About

Simple JSON to AST parser in TypeScript

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors