Visitor called by visit when parsing JSON.

The visitor functions have the following common parameters:

  • offset: Global offset within the JSON document, starting at 0
  • startLine: Line number, starting at 0
  • startCharacter: Start character (column) within the current line, starting at 0

Additionally some functions have a pathSupplier parameter which can be used to obtain the current JSONPath within the document.

interface JSONVisitor {
    onArrayBegin?: ((offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => boolean | void);
    onArrayEnd?: ((offset: number, length: number, startLine: number, startCharacter: number) => void);
    onComment?: ((offset: number, length: number, startLine: number, startCharacter: number) => void);
    onError?: ((error: ParseErrorCode, offset: number, length: number, startLine: number, startCharacter: number) => void);
    onLiteralValue?: ((value: any, offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => void);
    onObjectBegin?: ((offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => boolean | void);
    onObjectEnd?: ((offset: number, length: number, startLine: number, startCharacter: number) => void);
    onObjectProperty?: ((property: string, offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => void);
    onSeparator?: ((character: string, offset: number, length: number, startLine: number, startCharacter: number) => void);
}

Properties

onArrayBegin?: ((offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => boolean | void)

Invoked when an open bracket is encountered. The offset and length represent the location of the open bracket. When false is returned, the array items will not be visited.

onArrayEnd?: ((offset: number, length: number, startLine: number, startCharacter: number) => void)

Invoked when a closing bracket is encountered. The offset and length represent the location of the closing bracket.

onComment?: ((offset: number, length: number, startLine: number, startCharacter: number) => void)

When comments are allowed, invoked when a line or block comment is encountered. The offset and length represent the location of the comment.

onError?: ((error: ParseErrorCode, offset: number, length: number, startLine: number, startCharacter: number) => void)

Invoked on an error.

onLiteralValue?: ((value: any, offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => void)

Invoked when a literal value is encountered. The offset and length represent the location of the literal value.

onObjectBegin?: ((offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => boolean | void)

Invoked when an open brace is encountered and an object is started. The offset and length represent the location of the open brace. When false is returned, the object properties will not be visited.

onObjectEnd?: ((offset: number, length: number, startLine: number, startCharacter: number) => void)

Invoked when a closing brace is encountered and an object is completed. The offset and length represent the location of the closing brace.

onObjectProperty?: ((property: string, offset: number, length: number, startLine: number, startCharacter: number, pathSupplier: (() => JSONPath)) => void)

Invoked when a property is encountered. The offset and length represent the location of the property name. The JSONPath created by the pathSupplier refers to the enclosing JSON object, it does not include the property name yet.

onSeparator?: ((character: string, offset: number, length: number, startLine: number, startCharacter: number) => void)

Invoked when a comma or colon separator is encountered. The offset and length represent the location of the separator.