2 * Creates a JSON scanner on the given text.
3 * If ignoreTrivia is set, whitespaces or comments are ignored.
5 export declare const createScanner: (text: string, ignoreTrivia?: boolean) => JSONScanner;
6 export declare const enum ScanError {
8 UnexpectedEndOfComment = 1,
9 UnexpectedEndOfString = 2,
10 UnexpectedEndOfNumber = 3,
12 InvalidEscapeCharacter = 5,
15 export declare const enum SyntaxKind {
19 CloseBracketToken = 4,
27 LineCommentTrivia = 12,
28 BlockCommentTrivia = 13,
35 * The scanner object, representing a JSON scanner at a position in the input string.
37 export interface JSONScanner {
39 * Sets the scan position to a new offset. A call to 'scan' is needed to get the first token.
41 setPosition(pos: number): void;
43 * Read the next token. Returns the token code.
47 * Returns the current scan position, which is after the last read token.
49 getPosition(): number;
51 * Returns the last read token.
53 getToken(): SyntaxKind;
55 * Returns the last read token value. The value for strings is the decoded string content. For numbers it's of type number, for boolean it's true or false.
57 getTokenValue(): string;
59 * The start offset of the last read token.
61 getTokenOffset(): number;
63 * The length of the last read token.
65 getTokenLength(): number;
67 * The zero-based start line number of the last read token.
69 getTokenStartLine(): number;
71 * The zero-based start character (column) of the last read token.
73 getTokenStartCharacter(): number;
75 * An error code of the last scan.
77 getTokenError(): ScanError;
80 * 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.
82 export declare const getLocation: (text: string, position: number) => Location;
84 * 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.
85 * Therefore, always check the errors list to find out if the input was valid.
87 export declare const parse: (text: string, errors?: ParseError[], options?: ParseOptions) => any;
89 * 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.
91 export declare const parseTree: (text: string, errors?: ParseError[], options?: ParseOptions) => Node | undefined;
93 * Finds the node at the given path in a JSON DOM.
95 export declare const findNodeAtLocation: (root: Node, path: JSONPath) => Node | undefined;
97 * Finds the innermost node at the given offset. If includeRightBound is set, also finds nodes that end at the given offset.
99 export declare const findNodeAtOffset: (root: Node, offset: number, includeRightBound?: boolean) => Node | undefined;
101 * Gets the JSON path of the given JSON DOM node
103 export declare const getNodePath: (node: Node) => JSONPath;
105 * Evaluates the JavaScript object of the given JSON DOM node
107 export declare const getNodeValue: (node: Node) => any;
109 * Parses the given text and invokes the visitor functions for each object, array and literal reached.
111 export declare const visit: (text: string, visitor: JSONVisitor, options?: ParseOptions) => any;
113 * Takes JSON with JavaScript-style comments and remove
114 * them. Optionally replaces every none-newline character
115 * of comments with a replaceCharacter
117 export declare const stripComments: (text: string, replaceCh?: string) => string;
118 export interface ParseError {
119 error: ParseErrorCode;
123 export declare const enum ParseErrorCode {
125 InvalidNumberFormat = 2,
126 PropertyNameExpected = 3,
130 CloseBraceExpected = 7,
131 CloseBracketExpected = 8,
132 EndOfFileExpected = 9,
133 InvalidCommentToken = 10,
134 UnexpectedEndOfComment = 11,
135 UnexpectedEndOfString = 12,
136 UnexpectedEndOfNumber = 13,
138 InvalidEscapeCharacter = 15,
139 InvalidCharacter = 16
141 export declare function printParseErrorCode(code: ParseErrorCode): "InvalidSymbol" | "InvalidNumberFormat" | "PropertyNameExpected" | "ValueExpected" | "ColonExpected" | "CommaExpected" | "CloseBraceExpected" | "CloseBracketExpected" | "EndOfFileExpected" | "InvalidCommentToken" | "UnexpectedEndOfComment" | "UnexpectedEndOfString" | "UnexpectedEndOfNumber" | "InvalidUnicode" | "InvalidEscapeCharacter" | "InvalidCharacter" | "<unknown ParseErrorCode>";
142 export declare type NodeType = 'object' | 'array' | 'property' | 'string' | 'number' | 'boolean' | 'null';
143 export interface Node {
144 readonly type: NodeType;
145 readonly value?: any;
146 readonly offset: number;
147 readonly length: number;
148 readonly colonOffset?: number;
149 readonly parent?: Node;
150 readonly children?: Node[];
152 export declare type Segment = string | number;
153 export declare type JSONPath = Segment[];
154 export interface Location {
156 * The previous property key or literal value (string, number, boolean or null) or undefined.
160 * The path describing the location in the JSON document. The path consists of a sequence of strings
161 * representing an object property or numbers for array indices.
165 * Matches the locations path against a pattern consisting of strings (for properties) and numbers (for array indices).
166 * '*' will match a single segment of any property name or index.
167 * '**' will match a sequence of segments of any property name or index, or no segment.
169 matches: (patterns: JSONPath) => boolean;
171 * If set, the location's offset is at a property key.
173 isAtPropertyKey: boolean;
175 export interface ParseOptions {
176 disallowComments?: boolean;
177 allowTrailingComma?: boolean;
178 allowEmptyContent?: boolean;
180 export interface JSONVisitor {
182 * Invoked when an open brace is encountered and an object is started. The offset and length represent the location of the open brace.
184 onObjectBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
186 * Invoked when a property is encountered. The offset and length represent the location of the property name.
188 onObjectProperty?: (property: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
190 * Invoked when a closing brace is encountered and an object is completed. The offset and length represent the location of the closing brace.
192 onObjectEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
194 * Invoked when an open bracket is encountered. The offset and length represent the location of the open bracket.
196 onArrayBegin?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
198 * Invoked when a closing bracket is encountered. The offset and length represent the location of the closing bracket.
200 onArrayEnd?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
202 * Invoked when a literal value is encountered. The offset and length represent the location of the literal value.
204 onLiteralValue?: (value: any, offset: number, length: number, startLine: number, startCharacter: number) => void;
206 * Invoked when a comma or colon separator is encountered. The offset and length represent the location of the separator.
208 onSeparator?: (character: string, offset: number, length: number, startLine: number, startCharacter: number) => void;
210 * When comments are allowed, invoked when a line or block comment is encountered. The offset and length represent the location of the comment.
212 onComment?: (offset: number, length: number, startLine: number, startCharacter: number) => void;
214 * Invoked on an error.
216 onError?: (error: ParseErrorCode, offset: number, length: number, startLine: number, startCharacter: number) => void;
219 * Represents a text modification
221 export interface Edit {
223 * The start offset of the modification.
227 * The length of the modification. Must not be negative. Empty length represents an *insert*.
231 * The new content. Empty content represents a *remove*.
236 * A text range in the document
238 export interface Range {
240 * The start offset of the range.
244 * The length of the range. Must not be negative.
248 export interface FormattingOptions {
250 * If indentation is based on spaces (`insertSpaces` = true), the number of spaces that make an indent.
254 * Is indentation based on spaces?
256 insertSpaces?: boolean;
258 * The default 'end of line' character. If not set, '\n' is used as default.
262 * If set, will add a new line at the end of the document.
264 insertFinalNewline?: boolean;
267 * Computes the edits needed to format a JSON document.
269 * @param documentText The input text
270 * @param range The range to format or `undefined` to format the full content
271 * @param options The formatting options
272 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
273 * 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
274 * text in the original document. However, multiple edits can have
275 * 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.
276 * To apply edits to an input, you can use `applyEdits`.
278 export declare function format(documentText: string, range: Range | undefined, options: FormattingOptions): Edit[];
280 * Options used when computing the modification edits
282 export interface ModificationOptions {
284 * Formatting options. If undefined, the newly inserted code will be inserted unformatted.
286 formattingOptions?: FormattingOptions;
288 * Default false. If `JSONPath` refers to an index of an array and {@property isArrayInsertion} is `true`, then
289 * {@function modify} will insert a new item at that location instead of overwriting its contents.
291 isArrayInsertion?: boolean;
293 * Optional function to define the insertion index given an existing list of properties.
295 getInsertionIndex?: (properties: string[]) => number;
298 * Computes the edits needed to modify a value in the JSON document.
300 * @param documentText The input text
301 * @param path The path of the value to change. The path represents either to the document root, a property or an array item.
302 * If the path points to an non-existing property or item, it will be created.
303 * @param value The new value for the specified property or item. If the value is undefined,
304 * the property or item will be removed.
305 * @param options Options
306 * @returns A list of edit operations describing the formatting changes to the original document. Edits can be either inserts, replacements or
307 * 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
308 * text in the original document. However, multiple edits can have
309 * 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.
310 * To apply edits to an input, you can use `applyEdits`.
312 export declare function modify(text: string, path: JSONPath, value: any, options: ModificationOptions): Edit[];
314 * Applies edits to a input string.
316 export declare function applyEdits(text: string, edits: Edit[]): string;