What is jsonc-parser?
The jsonc-parser npm package is used for parsing and manipulating JSONC (JSON with Comments) files. It provides functionality to parse JSONC content, extract errors, and visit nodes within the JSONC structure. It also allows for modifications and formatting of the JSONC content.
What are jsonc-parser's main functionalities?
Parsing JSONC
This feature allows you to parse JSONC content, which includes comments, without stripping them out. It can handle both single-line and multi-line comments.
{"text": "// This is a comment\n{\"key\": \"value\"}"}
Extracting Errors
jsonc-parser can extract errors from JSONC content, which is useful for validating and providing feedback on the correctness of the JSONC.
{"text": "{\"key\": \"value\", // invalid comment\n}"}
Visiting Nodes
This feature allows you to visit nodes within the JSONC structure and perform operations or analysis on them, such as logging property names.
{"text": "{\"key\": {\"nestedKey\": \"nestedValue\"}}", "visitor": {"onObjectProperty": (property, visitContext) => { console.log(property); }}}
Modifying JSONC
jsonc-parser provides functionality to modify the JSONC content by specifying the path to the node and the new value.
{"text": "{\"key\": \"value\"}", "modifications": [{"path": [\"key\"], "value": \"newValue\"}]}
Formatting JSONC
This feature allows you to format JSONC content according to specified formatting options, such as using spaces or tabs for indentation.
{"text": "{\"key\":\"value\"}", "options": {"insertSpaces": true, "tabSize": 2}}
Other packages similar to jsonc-parser
strip-json-comments
This package is used to strip comments from JSON or JSONC content, allowing you to parse it with standard JSON parsers. It differs from jsonc-parser as it does not retain comments and does not provide functionality for visiting nodes or extracting errors.
comment-json
comment-json is similar to jsonc-parser in that it allows for parsing and stringifying JSONC. However, its API and feature set may differ, and it may not have the same focus on providing detailed error information or node visiting capabilities.
jsonc-parser
Scanner and parser for JSON with comments.
Why?
JSONC is JSON with JavaScript style comments. This node module provides a scanner and fault tolerant parser that can process JSONC but is also useful for standard JSON.
- the scanner tokenizes the input string into tokens and token offsets
- the parse function evaluates the JavaScipt object represented by JSON string in a fault tolerant fashion.
- the visit function implements a 'SAX' style parser with callbacks for the encountered properties and values
Installation
npm install --save jsonc-parser
API
Scanner:
export function createScanner(text:string, ignoreTrivia:boolean = false):JSONScanner;
export interface JSONScanner {
setPosition(pos: number): any;
scan(): SyntaxKind;
getPosition(): number;
getToken(): SyntaxKind;
getTokenValue(): string;
getTokenOffset(): number;
getTokenLength(): number;
getTokenError(): ScanError;
}
Parser:
export interface ParseOptions {
disallowComments?: boolean;
}
export declare function parse(text: string, errors?: {error: ParseErrorCode;}[], options?: ParseOptions): any;
export declare function visit(text: string, visitor: JSONVisitor, options?: ParseOptions): any;
export interface JSONVisitor {
onObjectBegin?: (offset: number, length: number) => void;
onObjectProperty?: (property: string, offset: number, length: number) => void;
onObjectEnd?: (offset: number, length: number) => void;
onArrayBegin?: (offset: number, length: number) => void;
onArrayEnd?: (offset: number, length: number) => void;
onLiteralValue?: (value: any, offset: number, length: number) => void;
onSeparator?: (charcter: string, offset: number, length: number) => void;
onError?: (error: ParseErrorCode, offset: number, length: number) => void;
}
Utilities:
export declare function stripComments(text: string, replaceCh?: string): string;
export declare function getLocation(text: string, position: number): Location;
License
(MIT License)
Copyright 2016, Microsoft