|
| 1 | +/** |
| 2 | + * Parses JSON string. |
| 3 | + * |
| 4 | + * @param source The JSON string to parse. |
| 5 | + * @param _ Unused - reveserved for reviver of JSON.parse. |
| 6 | + * @param options.bigint If true, parse large integers as BigInt |
| 7 | + * @returns Object with properties: |
| 8 | + * data: The parsed data. |
| 9 | + * pointers: An object where each key is a JSON pointer (RFC 6901), each |
| 10 | + * corresponding value is a Mapping object. |
| 11 | + */ |
| 12 | +export function parse( |
| 13 | + source: string, |
| 14 | + _?: unknown, |
| 15 | + options?: ParseOptions, |
| 16 | +): { |
| 17 | + data: unknown; |
| 18 | + pointers: Pointers; |
| 19 | +}; |
| 20 | + |
| 21 | +/** |
| 22 | + * Stringifies JavaScript data. |
| 23 | + * |
| 24 | + * @param data The data to convert to JSON. |
| 25 | + * @param _ Unused - reserved for replacer of JSON.stringify. |
| 26 | + * @param options If number or string, like the space parameter of |
| 27 | + * JSON.stringify, but if it is a string, it may only contain characters |
| 28 | + * space, tab ('\t'), caret return ('\r') and line feed ('\n') - using any |
| 29 | + * other character throws an exception. |
| 30 | + * @param options.space: Like above. |
| 31 | + * @param options.es6: If true, stringify ES6 Maps, Sets and Typed arrays as |
| 32 | + * JSON arrays. |
| 33 | + */ |
| 34 | +export function stringify( |
| 35 | + data: number | bigint | boolean | string | object, |
| 36 | + _?: unknown, |
| 37 | + options?: StringifyOptions | number | string, |
| 38 | +): { |
| 39 | + json: string; |
| 40 | + pointers: Pointers; |
| 41 | +}; |
| 42 | + |
| 43 | +export interface ParseOptions { |
| 44 | + /** Parse large integers as BigInt */ |
| 45 | + bigint?: boolean; |
| 46 | +} |
| 47 | + |
| 48 | +export interface StringifyOptions { |
| 49 | + /** |
| 50 | + * Like the space parameter of JSON.stringify, but if it is a string, it may |
| 51 | + * only contain characters space, tab ('\t'), caret return ('\r') and line |
| 52 | + * feed ('\n') - using any other character throws an exception. |
| 53 | + */ |
| 54 | + space?: number | string; |
| 55 | + |
| 56 | + /** Stringify ES6 Maps, Sets and Typed arrays (as JSON arrays). */ |
| 57 | + es6?: boolean; |
| 58 | +} |
| 59 | + |
| 60 | +export interface Pointers { |
| 61 | + /** |
| 62 | + * Each key is a JSON pointer |
| 63 | + * ([RFC 6901](https://datatracker.ietf.org/doc/html/rfc6901)). |
| 64 | + */ |
| 65 | + [pointer: string]: Mapping; |
| 66 | +} |
| 67 | + |
| 68 | +export interface Mapping { |
| 69 | + /** |
| 70 | + * Location of the beginning of the key in JSON string. This property is |
| 71 | + * only present if parent data is an object (rather than array). |
| 72 | + */ |
| 73 | + key?: Location; |
| 74 | + |
| 75 | + /** |
| 76 | + * Location of the end of the key in JSON string. This property is only |
| 77 | + * present if parent data is an object. |
| 78 | + */ |
| 79 | + keyEnd?: Location; |
| 80 | + |
| 81 | + /** Location of the beginning of the value in JSON string. */ |
| 82 | + value: Location; |
| 83 | + |
| 84 | + /** Location of the end of the value in JSON string. */ |
| 85 | + valueEnd: Location; |
| 86 | +} |
| 87 | + |
| 88 | +/** Location in the source string - zero-based numbers. */ |
| 89 | +export interface Location { |
| 90 | + /** Line number in JSON file. */ |
| 91 | + line: number; |
| 92 | + |
| 93 | + /** Column number in JSON string (from the beginning of line. */ |
| 94 | + column: number; |
| 95 | + |
| 96 | + /** Column number in JSON string (from the beginning of line. */ |
| 97 | + pos: number; |
| 98 | +} |
0 commit comments