A tolerant JSON parser
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:
- Creation of a
JsonDocument
root node and more formal AST structure - Support for inline comments
- Support for multi-line comments
- Support for trailing commas and many consecutive commas
- Include visitor pattern to visit the AST
- Include a limited error-recovery mode trying to catch (when
junker
set totrue
):- unclosed objects or arrays
- too many closing braces or brackets
- automatic comma injection
- support for unquoted keys
- Conversion to a native JavaScript object from
JsonNode
Basic examples are available to show how to use this package.
JSONish
The JSON parser accepts a superset of the JSON language:
// some comment
Install
npm install json-ast
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 JsonValueJsonString extends JsonValueJsonNumber extends JsonValueJsonTrue extends JsonValueJsonFalse extends JsonValueJsonNumber extends JsonValue
All the types exists in src/ast.js.
API
; // The visitor can stop at any time by assigning `Visitor.stop = true` { super; thiscomments = ; } { thiscomments; }; const JSON_BUFFER = `// Some comment{ "key": "value"`; // `verbose` will include the position in each nodeconst ast = ;; const visitor = ;ast;assert; // One can also the `JsonNode.toJSON` static method to convert to a JavaScript objectconst obj = JsonNode;;
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 defaultjunker
: enables an error recovery mode,false
by default
License
MIT Vlad Trushin and Romain Gaucher