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 declare function parse(text: string, errors?: {
error: ParseErrorCode;
}[]): any;
export declare function visit(text: string, visitor: JSONVisitor): 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