2.1.54 • Published 14 days ago

@anchan828/json-ast v2.1.54

Weekly downloads
735
License
MIT
Repository
github
Last release
14 days ago

A tolerant JSON parser

Test

Install

npm install @anchan828/json-ast

Features

The original code was developed by Vlad Trushin. Breaking modifications were made by Romain Gaucher to create a less strict JSON parser. Additionally, a more typical interaction with the AST has been implemented.

Current modifications and features as of 2.1.6 include:

Basic examples are available to show how to use this package.

JSONish

The JSON parser accepts a superset of the JSON language:

// some comment
{
  "key1": "value1", // some other comments
  "key2": "value2",
  ,
  ,
  /*
    Oh dear! It's important to put this here.
    And we love commas too!
    And we're missing the closing brace...
  */

Structure of the AST

As of 2.1.0, the AST is defined with the following types:

[JsonNode] // Essentially an abstract class
  position: [Position]

[JsonDocument] extends [JsonNode]
  child: [?]*
  comments: [JsonComment]*

[JsonValue] extends [JsonNode]
  value: [?]

[JsonObject] extends [JsonNode]
  properties: [JsonProperty]*
  comments: [JsonComment]*

[JsonProperty] extends [JsonNode]
  key: [JsonKey]
  value: [?]*

[JsonKey] extends [JsonValue]

[JsonArray]
  items: *
  comments: [JsonComment]*

[JsonComment] extends [JsonValue]
[JsonString] extends [JsonValue]
[JsonNumber] extends [JsonValue]
[JsonTrue] extends [JsonValue]
[JsonFalse] extends [JsonValue]
[JsonNumber] extends [JsonValue]

All the types exists in src/ast.ts.

API

import { parse, Visitor, AST } from "json-ast";

// The visitor can stop at any time by assigning `Visitor.stop = true`
class MyVisitor extends Visitor {
  private comments: string[] = []

  public comment(commentNode: JsonComment): void {
    this.comments.push(commentNode.value);
  }
}

const JSON_BUFFER = `// Some comment
{
  "key": "value"
`;

// `verbose` will include the position in each node
const ast = parse(JSON_BUFFER, { verbose: true, junker: true });
assert(ast instanceof JsonDocument);

const visitor = new MyVisitor();
ast.visit(visitor);
assert.deepEqual(visitor.comments, [" Some comment"]);

// One can also the `JsonNode.toJSON` static method to convert to a JavaScript object
const obj = toJSON(ast);
assert(obj.key === "value");

Parsing Options

The second argument of the parse function takes an object with the following settings:

  • verbose: include positions in each AST node, true by default
  • junker: enables an error recovery mode, false by default

License

MIT Vlad Trushin and Romain Gaucher and anchan828

2.1.54

14 days ago

2.1.52

28 days ago

2.1.53

21 days ago

2.1.51

1 month ago

2.1.49

2 months ago

2.1.47

2 months ago

2.1.48

2 months ago

2.1.45

3 months ago

2.1.46

2 months ago

2.1.50

1 month ago

2.1.43

3 months ago

2.1.44

3 months ago

2.1.41

3 months ago

2.1.42

3 months ago

2.1.40

4 months ago

2.1.38

4 months ago

2.1.39

4 months ago

2.1.36

5 months ago

2.1.37

4 months ago

2.1.34

5 months ago

2.1.35

5 months ago

2.1.32

6 months ago

2.1.33

5 months ago

2.1.31

6 months ago

2.1.27

7 months ago

2.1.28

7 months ago

2.1.29

6 months ago

2.1.30

6 months ago

2.1.26

7 months ago

2.1.25

7 months ago

2.1.23

8 months ago

2.1.24

7 months ago

2.1.22

8 months ago

2.1.21

8 months ago

2.1.20

8 months ago

2.1.19

9 months ago

2.1.18

9 months ago

2.1.17

9 months ago

2.1.16

9 months ago

2.1.15

10 months ago

2.1.14

10 months ago

2.1.13

1 year ago

2.1.12

2 years ago

2.1.10

2 years ago

2.1.8

2 years ago

2.1.11

2 years ago

2.1.9

2 years ago

2.1.7

2 years ago