Skip to content

DreamPast/efjson

Repository files navigation

efjson: A Stream-Based JSON Parser

English 简体中文

Features

  • no dependencies
  • stream parser requires minimal memory when no events are triggered

The state diagram for stream parsing JSON can be found in The Diagram of JSON Token State Transition.

Installation

Just copy efjson.ts into your project.

Example

Stream Parsing

import { JsonStreamParser } from "efjson";

const json = `
{
  "null": null,
  "true": true,
  "false": false,

  "string": "string",
  "string_with_escape": "string with \\"escape\\"",
  "string_with_unicode_escape": "string with \\uD83D\\uDE00",
  "string_with_unicode": "string with 😊",

  "integer": 1234,
  "negative": -1234,
  "number": 1234.5678,
  "number_with_exponent": 1.234e2,

  "array": [
    "this is the first element",
    {
      "object": "a nesting object"
    }
  ],
  "object": {
    "1st": [],
    "2st": {}
  }
}
`;
const parser = new JsonStreamParser();
// you can feed any length of string to the parser
for(const c of json) 
  console.log(parser.feed(c));
console.log(parser.end());

References

JSON Specification: RFC 4627 on JSON

JSON State Diagram: JSON

JSON5 Specification: The JSON5 Data Interchange Format

Releases

No releases published