2 Scanner and parser for JSON with comments.
4 [![npm Package](https://img.shields.io/npm/v/jsonc-parser.svg?style=flat-square)](https://www.npmjs.org/package/jsonc-parser)
5 [![NPM Downloads](https://img.shields.io/npm/dm/jsonc-parser.svg)](https://npmjs.org/package/jsonc-parser)
6 [![Build Status](https://travis-ci.org/Microsoft/node-jsonc-parser.svg?branch=master)](https://travis-ci.org/Microsoft/node-jsonc-parser)
10 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.
11 - the *scanner* tokenizes the input string into tokens and token offsets
12 - the *visit* function implements a 'SAX' style parser with callbacks for the encountered properties and values.
13 - the *parseTree* function computes a hierarchical DOM with offsets representing the encountered properties and values.
14 - the *parse* function evaluates the JavaScript object represented by JSON string in a fault tolerant fashion.
15 - the *getLocation* API returns a location object that describes the property or value located at a given offset in a JSON document.
16 - the *findNodeAtLocation* API finds the node at a given location path in a JSON DOM.
17 - the *format* API computes edits to format a JSON document.
18 - the *modify* API computes edits to insert, remove or replace a property or value in a JSON document.
19 - the *applyEdits* API applies edits to a document.
24 npm install --save jsonc-parser
34 * Creates a JSON scanner on the given text.
35 * If ignoreTrivia is set, whitespaces or comments are ignored.
37 export function createScanner(text:string, ignoreTrivia:boolean = false):JSONScanner;
40 * The scanner object, representing a JSON scanner at a position in the input string.
42 export interface JSONScanner {
44 * Sets the scan position to a new offset. A call to 'scan' is needed to get the first token.
46 setPosition(pos: number): any;
48 * Read the next token. Returns the token code.
52 * Returns the current scan position, which is after the last read token.
54 getPosition(): number;
56 * Returns the last read token.
58 getToken(): SyntaxKind;
60 * Returns the last read token value. The value for strings is the decoded string content. For numbers its of type number, for boolean it's true or false.
62 getTokenValue(): string;
64 * The start offset of the last read token.
66 getTokenOffset(): number;
68 * The length of the last read token.
70 getTokenLength(): number;
72 * The zero-based start line number of the last read token.
74 getTokenStartLine(): number;
76 * The zero-based start character (column) of the last read token.
78 getTokenStartCharacter(): number;
80 * An error code of the last scan.
82 getTokenError(): ScanError;
89 export interface ParseOptions {
90 disallowComments?: boolean;
93 * Parses the given text and returns the object the JSON content represents. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
94 * Therefore always check the errors list to find out if the input was valid.
96 export declare function parse(text: string, errors?: {error: ParseErrorCode;}[], options?: ParseOptions): any;
99 * Parses the given text and invokes the visitor functions for each object, array and literal reached.
101 export declare function visit(text: string, visitor: JSONVisitor, options?: ParseOptions): any;
103 export interface JSONVisitor {
105 * Invoked when an open brace is encountered and an object is started. The offset and length represent the location of the open brace.
107 onObjectBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
109 * Invoked when a property is encountered. The offset and length represent the location of the property name.
111 onObjectProperty?: (property: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
113 * Invoked when a closing brace is encountered and an object is completed. The offset and length represent the location of the closing brace.
115 onObjectEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
117 * Invoked when an open bracket is encountered. The offset and length represent the location of the open bracket.
119 onArrayBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
121 * Invoked when a closing bracket is encountered. The offset and length represent the location of the closing bracket.
123 onArrayEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
125 * Invoked when a literal value is encountered. The offset and length represent the location of the literal value.
127 onLiteralValue?: (value: any, offset: number, length: number, startLine: number, startCharacter: number) => void;
129 * Invoked when a comma or colon separator is encountered. The offset and length represent the location of the separator.
131 onSeparator?: (character: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
133 * When comments are allowed, invoked when a line or block comment is encountered. The offset and length represent the location of the comment.
135 onComment?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
137 * Invoked on an error.
139 onError?: (error: ParseErrorCode, offset: number, length: number, startLine: number, startCharacter: number) => void;
143 * Parses the given text and returns a tree representation the JSON content. On invalid input, the parser tries to be as fault tolerant as possible, but still return a result.
145 export declare function parseTree(text: string, errors?: ParseError[], options?: ParseOptions): Node | undefined;
147 export declare type NodeType = "object" | "array" | "property" | "string" | "number" | "boolean" | "null";
148 export interface Node {
153 colonOffset?: number;
163 * Takes JSON with JavaScript-style comments and remove
164 * them. Optionally replaces every none-newline character
165 * of comments with a replaceCharacter
167 export declare function stripComments(text: string, replaceCh?: string): string;
170 * For a given offset, evaluate the location in the JSON document. Each segment in the location path is either a property name or an array index.
172 export declare function getLocation(text: string, position: number): Location;
174 export declare type Segment = string | number;
175 export interface Location {
177 * The previous property key or literal value (string, number, boolean or null) or undefined.
181 * The path describing the location in the JSON document. The path consists of a sequence strings
182 * representing an object property or numbers for array indices.
186 * Matches the locations path against a pattern consisting of strings (for properties) and numbers (for array indices).
187 * '*' will match a single segment, of any property name or index.
188 * '**' will match a sequece of segments or no segment, of any property name or index.
190 matches: (patterns: Segment[]) => boolean;
192 * If set, the location's offset is at a property key.
194 isAtPropertyKey: boolean;
198 * Finds the node at the given path in a JSON DOM.
200 export function findNodeAtLocation(root: Node, path: JSONPath): Node | undefined;
203 * Finds the most inner node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
205 export function findNodeAtOffset(root: Node, offset: number, includeRightBound?: boolean) : Node | undefined;
208 * Gets the JSON path of the given JSON DOM node
210 export function getNodePath(node: Node) : JSONPath;
213 * Evaluates the JavaScript object of the given JSON DOM node
215 export function getNodeValue(node: Node): any;
218 * Computes the edits needed to format a JSON document.
220 * @param documentText The input text
221 * @param range The range to format or `undefined` to format the full content
222 * @param options The formatting options
223 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
224 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
225 * text in the original document. However, multiple edits can have
226 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
227 * To apply edits to an input, you can use `applyEdits`
229 export function format(documentText: string, range: Range, options: FormattingOptions): Edit[];
233 * Computes the edits needed to modify a value in the JSON document.
235 * @param documentText The input text
236 * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
237 * If the path points to an non-existing property or item, it will be created.
238 * @param value The new value for the specified property or item. If the value is undefined,
239 * the property or item will be removed.
240 * @param options Options
241 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
242 * removals of text segments. All offsets refer to the original state of the document. No two edits must change or remove the same range of
243 * text in the original document. However, multiple edits can have
244 * the same offset, for example multiple inserts, or an insert followed by a remove or replace. The order in the array defines which edit is applied first.
245 * To apply edits to an input, you can use `applyEdits`
247 export function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[];
250 * Applies edits to a input string.
252 export function applyEdits(text: string, edits: Edit[]): string;
255 * Represents a text modification
257 export interface Edit {
259 * The start offset of the modification.
263 * The length of the modification. Must not be negative. Empty length represents an *insert*.
267 * The new content. Empty content represents a *remove*.
273 * A text range in the document
275 export interface Range {
277 * The start offset of the range.
281 * The length of the range. Must not be negative.
286 export interface FormattingOptions {
288 * If indentation is based on spaces (`insertSpaces` = true), then what is the number of spaces that make an indent?
292 * Is indentation based on spaces?
294 insertSpaces: boolean;
296 * The default 'end of line' character
309 Copyright 2018, Microsoft