liudong
2023-05-29 340f156319b863525e50e900c58e59b86ecb3d5e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
type ParseMode = 'json' | 'cjson' | 'json5'
 
interface ParseOptions {
  ignoreComments?: boolean
  ignoreTrailingCommas?: boolean
  allowSingleQuotedStrings?: boolean
  allowDuplicateObjectKeys?: boolean
  mode?: ParseMode
  reviver?: Function
}
 
/**
 * Parses a string formatted as JSON to a JSON output (primitive type, object
 * or array). It is compatible with the native `JSON.parse` method.
 *
 * @example
 * ```ts
 * import { parse } from '@prantlf/jsonlint'
 * const parsed = parse('string with JSON data')
 * ```
 *
 * @param input - a string input to parse
 * @param reviverOrOptions - either a value reviver or an object
 *                           with multiple options
 * @returns the parsed result - a primitive value, array or object
 */
declare function parse (input: string, reviverOrOptions?: Function | ParseOptions): object
 
interface TokenizeOptions {
  ignoreComments?: boolean
  ignoreTrailingCommas?: boolean
  allowSingleQuotedStrings?: boolean
  allowDuplicateObjectKeys?: boolean
  mode?: ParseMode
  reviver?: Function
  rawTokens?: boolean
  tokenLocations?: boolean
  tokenPaths?: boolean
}
 
/**
 * Parses a string formatted as JSON to an array of JSON tokens.
 *
 * @example
 * ```ts
 * import { tokenize } from '@prantlf/jsonlint'
 * const tokens = tokenize('string with JSON data')
 * ```
 *
 * @param input - a string input to parse
 * @param reviverOrOptions - either a value reviver or an object
 *                           with multiple options
 * @returns an array with the tokens
 */
declare function tokenize (input: string, reviverOrOptions?: Function | TokenizeOptions): object
 
declare module '@prantlf/jsonlint/lib/validator' {
  type Environment = 'json-schema-draft-04' | 'json-schema-draft-06' | 'json-schema-draft-07'
 
  interface CompileOptions {
    ignoreComments?: boolean
    ignoreTrailingCommas?: boolean
    allowSingleQuotedStrings?: boolean
    allowDuplicateObjectKeys?: boolean
    environment?: Environment
    mode?: ParseMode
  }
 
  /**
   * Generates a JSON Schema validator.
   *
   * @example
   * ```ts
   * import { compile } from '@prantlf/jsonlint/lib/validator'
   * const validate = compile('string with JSON schema')
   * const parsed = validate('string with JSON data')
   * ```
   *
   * @param schema - a string with the JSON Schema to validate with
   * @param environmentOrOptions - either a string with the version
   *                               of the JSON Schema standard or an object
   *                               with multiple options
   * @returns the validator function
   */
  function compile (schema: string, environmentOrOptions?: Environment | CompileOptions): Function
}
 
declare module '@prantlf/jsonlint/lib/printer' {
  interface PrintOptions {
    indent?: number | string
    pruneComments?: boolean
    stripObjectKeys?: boolean
    enforceDoubleQuotes?: boolean
    enforceSingleQuotes?: boolean
    trimTrailingCommas?: boolean
  }
 
  /**
   * Pretty-prints an array of JSON tokens parsed from a valid JSON string by `tokenize`.
   *
   * @example
   * ```ts
   * import { tokenize } from '@prantlf/jsonlint'
   * import { print } from '@prantlf/jsonlint/lib/printer'
   * const tokens = tokenize('string with JSON data', { rawTokens: true })
   * const outputString = print(tokens, { indent: 2 })
   * ```
   *
   * @param tokens - an array of JSON tokens
   * @param options - an object with multiple options
   * @returns the output string
   */
  function print (tokens: Array<object>, options?: PrintOptions): string
}
 
export { parse, tokenize }