@helpers4/json 0.0.0-stage → 3.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +160 -0
- package/README.md +96 -2
- package/lib/index.d.ts +463 -0
- package/lib/index.js +928 -0
- package/lib/index.js.map +1 -0
- package/llms.txt +699 -0
- package/meta/api.json +662 -0
- package/meta/category.json +6 -0
- package/meta/examples.json +185 -0
- package/meta/licenses.json +4 -0
- package/package.json +52 -3
package/meta/api.json
ADDED
|
@@ -0,0 +1,662 @@
|
|
|
1
|
+
{
|
|
2
|
+
"category": "json",
|
|
3
|
+
"version": "3.1.3",
|
|
4
|
+
"functions": [
|
|
5
|
+
{
|
|
6
|
+
"name": "parse",
|
|
7
|
+
"kind": "function",
|
|
8
|
+
"description": "Parses JSON permissively: strict JSON, JSONC (the dialect of `tsconfig.json` and VS Code's\n`settings.json`, also known as HuJSON or JWCC) and JSON5 — each a superset of the one before.\n\n**Accepts:**\n- everything parseStrict accepts (strict JSON, RFC 8259)\n- comments: `// …` to the end of the line, and `/* … */`\n- one trailing comma after the last item of an object or array\n- object keys without quotes, when they are ECMAScript identifiers (`{ name: 1 }`, `{ $id: 1 }`)\n- strings in single quotes, and in either quote: the escapes `\\'`, `\\v`, `\\0` and `\\xHH`, any\n other escaped character standing for itself, and a backslash before a line break to continue\n the string on the next line\n- numbers with a leading `+`, a leading or trailing dot (`.5`, `5.`), in hexadecimal (`0x1F`),\n and `Infinity`, `-Infinity` and `NaN`\n- any Unicode whitespace between tokens, including a leading byte order mark (U+FEFF)\n\n**Rejects:** more than one trailing comma in a row, leading zeros (`01`), octal or binary\nnumbers, unquoted keys that aren't identifiers (`{ my-key: 1 }`), raw line breaks inside a\nstring, an unterminated comment or string, `undefined`, and more than one value.\n\n**On invalid input:** throws a `SyntaxError` naming the line and column of the first problem.\n\nStrict JSON goes straight to the native `JSON.parse`, and JSONC to `JSON.parse` after its\ncomments are blanked out; only text that is neither goes through the JSON5 reader (written for\nhelpers4 from the JSON5 1.0.0 specification), so the common cases cost nothing extra.\n\nValues are built as `JSON.parse` builds them: the last of duplicate keys wins, and a\n`__proto__` key is an own property, never the prototype. `NaN` and `Infinity` have no JSON\nrepresentation — stringify writes them as `null`.",
|
|
9
|
+
"since": "3.1.3",
|
|
10
|
+
"signatures": [
|
|
11
|
+
{
|
|
12
|
+
"signature": "parse<T = unknown>(text: string): T",
|
|
13
|
+
"description": "Parses JSON permissively: strict JSON, JSONC (the dialect of `tsconfig.json` and VS Code's\n`settings.json`, also known as HuJSON or JWCC) and JSON5 — each a superset of the one before.\n\n**Accepts:**\n- everything parseStrict accepts (strict JSON, RFC 8259)\n- comments: `// …` to the end of the line, and `/* … */`\n- one trailing comma after the last item of an object or array\n- object keys without quotes, when they are ECMAScript identifiers (`{ name: 1 }`, `{ $id: 1 }`)\n- strings in single quotes, and in either quote: the escapes `\\'`, `\\v`, `\\0` and `\\xHH`, any\n other escaped character standing for itself, and a backslash before a line break to continue\n the string on the next line\n- numbers with a leading `+`, a leading or trailing dot (`.5`, `5.`), in hexadecimal (`0x1F`),\n and `Infinity`, `-Infinity` and `NaN`\n- any Unicode whitespace between tokens, including a leading byte order mark (U+FEFF)\n\n**Rejects:** more than one trailing comma in a row, leading zeros (`01`), octal or binary\nnumbers, unquoted keys that aren't identifiers (`{ my-key: 1 }`), raw line breaks inside a\nstring, an unterminated comment or string, `undefined`, and more than one value.\n\n**On invalid input:** throws a `SyntaxError` naming the line and column of the first problem.\n\nStrict JSON goes straight to the native `JSON.parse`, and JSONC to `JSON.parse` after its\ncomments are blanked out; only text that is neither goes through the JSON5 reader (written for\nhelpers4 from the JSON5 1.0.0 specification), so the common cases cost nothing extra.\n\nValues are built as `JSON.parse` builds them: the last of duplicate keys wins, and a\n`__proto__` key is an own property, never the prototype. `NaN` and `Infinity` have no JSON\nrepresentation — stringify writes them as `null`.",
|
|
14
|
+
"params": [
|
|
15
|
+
{
|
|
16
|
+
"name": "text",
|
|
17
|
+
"type": "string",
|
|
18
|
+
"description": "The JSON, JSONC or JSON5 text to parse"
|
|
19
|
+
}
|
|
20
|
+
],
|
|
21
|
+
"returns": {
|
|
22
|
+
"type": "T",
|
|
23
|
+
"description": "The parsed value, typed as `T`"
|
|
24
|
+
},
|
|
25
|
+
"typeParameters": [
|
|
26
|
+
{
|
|
27
|
+
"name": "T",
|
|
28
|
+
"default": "unknown"
|
|
29
|
+
}
|
|
30
|
+
]
|
|
31
|
+
}
|
|
32
|
+
],
|
|
33
|
+
"examples": [
|
|
34
|
+
{
|
|
35
|
+
"title": "Parse a config file with comments",
|
|
36
|
+
"description": "Reads JSONC — the tsconfig.json / settings.json dialect — as well as strict JSON.",
|
|
37
|
+
"code": "parse(`{\n // where the build goes\n \"outDir\": \"dist\",\n}`)\n// => { outDir: 'dist' }"
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
"title": "Invalid input throws",
|
|
41
|
+
"description": "The SyntaxError names the line and column of the problem.",
|
|
42
|
+
"code": "parse('{ my-key: 1 }')\n// throws SyntaxError: a key with a dash needs quotes"
|
|
43
|
+
},
|
|
44
|
+
{
|
|
45
|
+
"title": "Parse JSON5",
|
|
46
|
+
"description": "Unquoted keys, single quotes, hexadecimal and leading-dot numbers, trailing commas.",
|
|
47
|
+
"code": "parse(\"{ name: 'app', retries: 0x3, ratio: .5, }\")\n// => { name: 'app', retries: 3, ratio: 0.5 }"
|
|
48
|
+
}
|
|
49
|
+
],
|
|
50
|
+
"sourceFile": "parse.ts"
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
"name": "parseLines",
|
|
54
|
+
"kind": "function",
|
|
55
|
+
"description": "Parses newline-delimited JSON — one JSON value per line. Covers both NDJSON\n(`application/x-ndjson`, `.ndjson`) and JSON Lines (`.jsonl`), which are the same format.\n\n**Accepts:**\n- one strict JSON value per line (each line is read like parseStrict)\n- `\\n` and `\\r\\n` line endings\n- a trailing line break after the last value, or none\n- blank lines, skipped by default (see ParseLinesOptions.blankLines)\n- an empty text, which holds zero values\n\n**Rejects:** comments, trailing commas and any other non-strict JSON on a line, a value spread\nover several lines, and a leading byte order mark (JSON Lines forbids it).\n\n**On invalid input:** throws a `SyntaxError` naming the 1-based line number, with the\noriginal parse error as its `cause` — unless `invalidLines`/`blankLines` say to skip it.",
|
|
56
|
+
"since": "3.1.3",
|
|
57
|
+
"signatures": [
|
|
58
|
+
{
|
|
59
|
+
"signature": "parseLines<T = unknown>(text: string, options: ParseLinesOptions): T[]",
|
|
60
|
+
"description": "Parses newline-delimited JSON — one JSON value per line. Covers both NDJSON\n(`application/x-ndjson`, `.ndjson`) and JSON Lines (`.jsonl`), which are the same format.\n\n**Accepts:**\n- one strict JSON value per line (each line is read like parseStrict)\n- `\\n` and `\\r\\n` line endings\n- a trailing line break after the last value, or none\n- blank lines, skipped by default (see ParseLinesOptions.blankLines)\n- an empty text, which holds zero values\n\n**Rejects:** comments, trailing commas and any other non-strict JSON on a line, a value spread\nover several lines, and a leading byte order mark (JSON Lines forbids it).\n\n**On invalid input:** throws a `SyntaxError` naming the 1-based line number, with the\noriginal parse error as its `cause` — unless `invalidLines`/`blankLines` say to skip it.",
|
|
61
|
+
"params": [
|
|
62
|
+
{
|
|
63
|
+
"name": "text",
|
|
64
|
+
"type": "string",
|
|
65
|
+
"description": "The newline-delimited JSON text"
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
"name": "options",
|
|
69
|
+
"type": "ParseLinesOptions",
|
|
70
|
+
"description": "How to handle blank and invalid lines — see ParseLinesOptions",
|
|
71
|
+
"defaultValue": "{}"
|
|
72
|
+
}
|
|
73
|
+
],
|
|
74
|
+
"returns": {
|
|
75
|
+
"type": "T[]",
|
|
76
|
+
"description": "The parsed values, in line order, typed as `T[]`"
|
|
77
|
+
},
|
|
78
|
+
"typeParameters": [
|
|
79
|
+
{
|
|
80
|
+
"name": "T",
|
|
81
|
+
"default": "unknown"
|
|
82
|
+
}
|
|
83
|
+
]
|
|
84
|
+
}
|
|
85
|
+
],
|
|
86
|
+
"examples": [
|
|
87
|
+
{
|
|
88
|
+
"title": "Read NDJSON / JSON Lines",
|
|
89
|
+
"description": "One JSON value per line; a trailing line break is optional.",
|
|
90
|
+
"code": "parseLines('{\"id\":1}\\n{\"id\":2}\\n')\n// => [{ id: 1 }, { id: 2 }]"
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
"title": "Skip a truncated last line",
|
|
94
|
+
"description": "With invalidLines: \"skip\", a line cut by a crash is dropped instead of failing the whole parse.",
|
|
95
|
+
"code": "parseLines('{\"id\":1}\\n{\"id\":', { invalidLines: 'skip' })\n// => [{ id: 1 }]"
|
|
96
|
+
},
|
|
97
|
+
{
|
|
98
|
+
"title": "Fail on an invalid line by default",
|
|
99
|
+
"description": "The SyntaxError names the line.",
|
|
100
|
+
"code": "parseLines('1\\n{\\n3')\n// throws SyntaxError: parseLines: line 2: ..."
|
|
101
|
+
}
|
|
102
|
+
],
|
|
103
|
+
"sourceFile": "parseLines.ts",
|
|
104
|
+
"relatedTypes": [
|
|
105
|
+
{
|
|
106
|
+
"name": "ParseLinesOptions",
|
|
107
|
+
"description": "Options for parseLines and safeParseLines.",
|
|
108
|
+
"typeDefinition": "interface ParseLinesOptions {\n blankLines?: \"skip\" | \"fail\";\n invalidLines?: \"skip\" | \"fail\";\n}"
|
|
109
|
+
}
|
|
110
|
+
]
|
|
111
|
+
},
|
|
112
|
+
{
|
|
113
|
+
"name": "parseSequence",
|
|
114
|
+
"kind": "function",
|
|
115
|
+
"description": "Parses a JSON text sequence (RFC 7464, `application/json-seq`): records that each start with\nthe ASCII record separator (RS, U+001E) and hold one JSON text, as written by `jq --seq`.\n\n**Accepts:**\n- one strict JSON value per record (each record is read like parseStrict)\n- JSON values spread over several lines inside a record — unlike parseLines\n- several RS in a row (empty records are ignored, as the RFC's grammar allows)\n- an empty text, which holds zero records\n\n**Rejects (as invalid records):**\n- anything before the first RS\n- a record that isn't strict JSON, or holds only whitespace\n- a top-level number, `true`, `false` or `null` not followed by whitespace — RFC 7464 §2.4\n requires dropping it, since it may have been truncated\n\n**On invalid input:** throws a `SyntaxError` naming the 1-based record number, with the\noriginal parse error (if any) as its `cause` — unless `invalidRecords: 'skip'` says to drop it.",
|
|
116
|
+
"since": "3.1.3",
|
|
117
|
+
"signatures": [
|
|
118
|
+
{
|
|
119
|
+
"signature": "parseSequence<T = unknown>(text: string, options: ParseSequenceOptions): T[]",
|
|
120
|
+
"description": "Parses a JSON text sequence (RFC 7464, `application/json-seq`): records that each start with\nthe ASCII record separator (RS, U+001E) and hold one JSON text, as written by `jq --seq`.\n\n**Accepts:**\n- one strict JSON value per record (each record is read like parseStrict)\n- JSON values spread over several lines inside a record — unlike parseLines\n- several RS in a row (empty records are ignored, as the RFC's grammar allows)\n- an empty text, which holds zero records\n\n**Rejects (as invalid records):**\n- anything before the first RS\n- a record that isn't strict JSON, or holds only whitespace\n- a top-level number, `true`, `false` or `null` not followed by whitespace — RFC 7464 §2.4\n requires dropping it, since it may have been truncated\n\n**On invalid input:** throws a `SyntaxError` naming the 1-based record number, with the\noriginal parse error (if any) as its `cause` — unless `invalidRecords: 'skip'` says to drop it.",
|
|
121
|
+
"params": [
|
|
122
|
+
{
|
|
123
|
+
"name": "text",
|
|
124
|
+
"type": "string",
|
|
125
|
+
"description": "The JSON text sequence"
|
|
126
|
+
},
|
|
127
|
+
{
|
|
128
|
+
"name": "options",
|
|
129
|
+
"type": "ParseSequenceOptions",
|
|
130
|
+
"description": "How to handle invalid records — see ParseSequenceOptions",
|
|
131
|
+
"defaultValue": "{}"
|
|
132
|
+
}
|
|
133
|
+
],
|
|
134
|
+
"returns": {
|
|
135
|
+
"type": "T[]",
|
|
136
|
+
"description": "The parsed values, in record order, typed as `T[]`"
|
|
137
|
+
},
|
|
138
|
+
"typeParameters": [
|
|
139
|
+
{
|
|
140
|
+
"name": "T",
|
|
141
|
+
"default": "unknown"
|
|
142
|
+
}
|
|
143
|
+
]
|
|
144
|
+
}
|
|
145
|
+
],
|
|
146
|
+
"examples": [
|
|
147
|
+
{
|
|
148
|
+
"title": "Read a JSON text sequence",
|
|
149
|
+
"description": "Each record starts with the record separator U+001E, as jq --seq writes it.",
|
|
150
|
+
"code": "parseSequence('\\u001E{\"id\":1}\\n\\u001E{\"id\":2}\\n')\n// => [{ id: 1 }, { id: 2 }]"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"title": "Recover from a truncated log",
|
|
154
|
+
"description": "RFC 7464 recommends skipping invalid records; ask for it with invalidRecords: \"skip\".",
|
|
155
|
+
"code": "parseSequence('\\u001E{\"id\":1}\\n\\u001E{\"id', { invalidRecords: 'skip' })\n// => [{ id: 1 }]"
|
|
156
|
+
}
|
|
157
|
+
],
|
|
158
|
+
"sourceFile": "parseSequence.ts",
|
|
159
|
+
"relatedTypes": [
|
|
160
|
+
{
|
|
161
|
+
"name": "ParseSequenceOptions",
|
|
162
|
+
"description": "Options for parseSequence and safeParseSequence.",
|
|
163
|
+
"typeDefinition": "interface ParseSequenceOptions {\n invalidRecords?: \"skip\" | \"fail\";\n}"
|
|
164
|
+
}
|
|
165
|
+
]
|
|
166
|
+
},
|
|
167
|
+
{
|
|
168
|
+
"name": "parseStrict",
|
|
169
|
+
"kind": "function",
|
|
170
|
+
"description": "Parses strict JSON (RFC 8259) — exactly what `JSON.parse` accepts, nothing more.\n\n**Accepts:** standard JSON only — double-quoted strings and keys, finite decimal numbers,\n`true`/`false`/`null`, objects and arrays, surrounding whitespace.\n\n**Rejects:** comments, trailing commas, single quotes, unquoted keys, hexadecimal numbers,\n`NaN`/`Infinity`, a leading byte order mark (U+FEFF), and anything else that isn't strict JSON.\n\n**On invalid input:** throws the native `SyntaxError` from `JSON.parse`.\n\nUse it to validate input that must be strict JSON, e.g. an API payload. To also read config\nfiles with comments or JSON5, use parse. To never throw, use safeParseStrict.",
|
|
171
|
+
"since": "3.1.3",
|
|
172
|
+
"signatures": [
|
|
173
|
+
{
|
|
174
|
+
"signature": "parseStrict<T = unknown>(text: string): T",
|
|
175
|
+
"description": "Parses strict JSON (RFC 8259) — exactly what `JSON.parse` accepts, nothing more.\n\n**Accepts:** standard JSON only — double-quoted strings and keys, finite decimal numbers,\n`true`/`false`/`null`, objects and arrays, surrounding whitespace.\n\n**Rejects:** comments, trailing commas, single quotes, unquoted keys, hexadecimal numbers,\n`NaN`/`Infinity`, a leading byte order mark (U+FEFF), and anything else that isn't strict JSON.\n\n**On invalid input:** throws the native `SyntaxError` from `JSON.parse`.\n\nUse it to validate input that must be strict JSON, e.g. an API payload. To also read config\nfiles with comments or JSON5, use parse. To never throw, use safeParseStrict.",
|
|
176
|
+
"params": [
|
|
177
|
+
{
|
|
178
|
+
"name": "text",
|
|
179
|
+
"type": "string",
|
|
180
|
+
"description": "The JSON text to parse"
|
|
181
|
+
}
|
|
182
|
+
],
|
|
183
|
+
"returns": {
|
|
184
|
+
"type": "T",
|
|
185
|
+
"description": "The parsed value, typed as `T`"
|
|
186
|
+
},
|
|
187
|
+
"typeParameters": [
|
|
188
|
+
{
|
|
189
|
+
"name": "T",
|
|
190
|
+
"default": "unknown"
|
|
191
|
+
}
|
|
192
|
+
]
|
|
193
|
+
}
|
|
194
|
+
],
|
|
195
|
+
"examples": [
|
|
196
|
+
{
|
|
197
|
+
"title": "Parse strict JSON",
|
|
198
|
+
"description": "Exactly what JSON.parse accepts, typed with a generic.",
|
|
199
|
+
"code": "parseStrict<{ id: number }>('{\"id\":1}')\n// => { id: 1 }"
|
|
200
|
+
},
|
|
201
|
+
{
|
|
202
|
+
"title": "Reject anything that is not strict JSON",
|
|
203
|
+
"description": "Comments and trailing commas throw a SyntaxError: use it to validate an API payload.",
|
|
204
|
+
"code": "parseStrict('{\"id\":1,}')\n// throws SyntaxError"
|
|
205
|
+
}
|
|
206
|
+
],
|
|
207
|
+
"sourceFile": "parseStrict.ts"
|
|
208
|
+
},
|
|
209
|
+
{
|
|
210
|
+
"name": "safeParse",
|
|
211
|
+
"kind": "function",
|
|
212
|
+
"description": "Parses JSON permissively like parse — strict JSON, JSONC (HuJSON, JWCC) and JSON5 —\nbut returns `null` (or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parse does — see there for the full list.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
213
|
+
"since": "3.1.3",
|
|
214
|
+
"signatures": [
|
|
215
|
+
{
|
|
216
|
+
"signature": "safeParse<T = unknown>(text: string): T | null",
|
|
217
|
+
"description": "Parses JSON permissively like parse — strict JSON, JSONC (HuJSON, JWCC) and JSON5 —\nbut returns `null` (or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parse does — see there for the full list.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
218
|
+
"params": [
|
|
219
|
+
{
|
|
220
|
+
"name": "text",
|
|
221
|
+
"type": "string",
|
|
222
|
+
"description": "The JSON, JSONC or JSON5 text to parse"
|
|
223
|
+
}
|
|
224
|
+
],
|
|
225
|
+
"returns": {
|
|
226
|
+
"type": "T | null",
|
|
227
|
+
"description": "The parsed value typed as `T`, or `fallback` on invalid input"
|
|
228
|
+
},
|
|
229
|
+
"typeParameters": [
|
|
230
|
+
{
|
|
231
|
+
"name": "T",
|
|
232
|
+
"default": "unknown"
|
|
233
|
+
}
|
|
234
|
+
]
|
|
235
|
+
},
|
|
236
|
+
{
|
|
237
|
+
"signature": "safeParse<T = unknown, F = T>(text: string, fallback: F): T | F",
|
|
238
|
+
"description": "Parses JSON permissively like parse — strict JSON, JSONC (HuJSON, JWCC) and JSON5 —\nbut returns `null` (or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parse does — see there for the full list.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
239
|
+
"params": [
|
|
240
|
+
{
|
|
241
|
+
"name": "text",
|
|
242
|
+
"type": "string",
|
|
243
|
+
"description": "The JSON, JSONC or JSON5 text to parse"
|
|
244
|
+
},
|
|
245
|
+
{
|
|
246
|
+
"name": "fallback",
|
|
247
|
+
"type": "F",
|
|
248
|
+
"description": "Value returned on invalid input. Defaults to `null` when omitted."
|
|
249
|
+
}
|
|
250
|
+
],
|
|
251
|
+
"returns": {
|
|
252
|
+
"type": "T | F",
|
|
253
|
+
"description": "The parsed value typed as `T`, or `fallback` on invalid input"
|
|
254
|
+
},
|
|
255
|
+
"typeParameters": [
|
|
256
|
+
{
|
|
257
|
+
"name": "T",
|
|
258
|
+
"default": "unknown"
|
|
259
|
+
},
|
|
260
|
+
{
|
|
261
|
+
"name": "F",
|
|
262
|
+
"default": "T"
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
],
|
|
267
|
+
"examples": [
|
|
268
|
+
{
|
|
269
|
+
"title": "Read JSONC or JSON5 without throwing",
|
|
270
|
+
"description": "Same parsing as parse, but returns null on invalid input.",
|
|
271
|
+
"code": "safeParse('{\"a\": 1, /* note */}') // => { a: 1 }\nsafeParse('{a: }') // => null"
|
|
272
|
+
},
|
|
273
|
+
{
|
|
274
|
+
"title": "Fall back to defaults",
|
|
275
|
+
"description": "Pass a fallback to get a usable value even when the file is broken.",
|
|
276
|
+
"code": "safeParse('{ broken', {})\n// => {}"
|
|
277
|
+
}
|
|
278
|
+
],
|
|
279
|
+
"sourceFile": "safeParse.ts"
|
|
280
|
+
},
|
|
281
|
+
{
|
|
282
|
+
"name": "safeParseLines",
|
|
283
|
+
"kind": "function",
|
|
284
|
+
"description": "Parses newline-delimited JSON (NDJSON, JSON Lines) like parseLines, but returns `null`\n(or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseLines does, with the same\nParseLinesOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
285
|
+
"since": "3.1.3",
|
|
286
|
+
"signatures": [
|
|
287
|
+
{
|
|
288
|
+
"signature": "safeParseLines<T = unknown>(text: string): T[] | null",
|
|
289
|
+
"description": "Parses newline-delimited JSON (NDJSON, JSON Lines) like parseLines, but returns `null`\n(or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseLines does, with the same\nParseLinesOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
290
|
+
"params": [
|
|
291
|
+
{
|
|
292
|
+
"name": "text",
|
|
293
|
+
"type": "string",
|
|
294
|
+
"description": "The newline-delimited JSON text"
|
|
295
|
+
}
|
|
296
|
+
],
|
|
297
|
+
"returns": {
|
|
298
|
+
"type": "T[] | null",
|
|
299
|
+
"description": "The parsed values typed as `T[]`, or `fallback` on invalid input"
|
|
300
|
+
},
|
|
301
|
+
"typeParameters": [
|
|
302
|
+
{
|
|
303
|
+
"name": "T",
|
|
304
|
+
"default": "unknown"
|
|
305
|
+
}
|
|
306
|
+
]
|
|
307
|
+
},
|
|
308
|
+
{
|
|
309
|
+
"signature": "safeParseLines<T = unknown, F = T[]>(text: string, fallback: F, options?: ParseLinesOptions): F | T[]",
|
|
310
|
+
"description": "Parses newline-delimited JSON (NDJSON, JSON Lines) like parseLines, but returns `null`\n(or `fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseLines does, with the same\nParseLinesOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
311
|
+
"params": [
|
|
312
|
+
{
|
|
313
|
+
"name": "text",
|
|
314
|
+
"type": "string",
|
|
315
|
+
"description": "The newline-delimited JSON text"
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
"name": "fallback",
|
|
319
|
+
"type": "F",
|
|
320
|
+
"description": "Value returned on invalid input. Defaults to `null` when omitted."
|
|
321
|
+
},
|
|
322
|
+
{
|
|
323
|
+
"name": "options",
|
|
324
|
+
"type": "ParseLinesOptions",
|
|
325
|
+
"description": "How to handle blank and invalid lines — see ParseLinesOptions",
|
|
326
|
+
"optional": true
|
|
327
|
+
}
|
|
328
|
+
],
|
|
329
|
+
"returns": {
|
|
330
|
+
"type": "F | T[]",
|
|
331
|
+
"description": "The parsed values typed as `T[]`, or `fallback` on invalid input"
|
|
332
|
+
},
|
|
333
|
+
"typeParameters": [
|
|
334
|
+
{
|
|
335
|
+
"name": "T",
|
|
336
|
+
"default": "unknown"
|
|
337
|
+
},
|
|
338
|
+
{
|
|
339
|
+
"name": "F",
|
|
340
|
+
"default": "T[]"
|
|
341
|
+
}
|
|
342
|
+
]
|
|
343
|
+
}
|
|
344
|
+
],
|
|
345
|
+
"examples": [
|
|
346
|
+
{
|
|
347
|
+
"title": "Read NDJSON without throwing",
|
|
348
|
+
"description": "Returns null if any line is invalid.",
|
|
349
|
+
"code": "safeParseLines('1\\n2\\n') // => [1, 2]\nsafeParseLines('1\\n{') // => null"
|
|
350
|
+
},
|
|
351
|
+
{
|
|
352
|
+
"title": "Options without a fallback",
|
|
353
|
+
"description": "Pass null as the fallback to give options.",
|
|
354
|
+
"code": "safeParseLines('1\\n\\n2', null, { blankLines: 'fail' })\n// => null (JSON Lines forbids blank lines)"
|
|
355
|
+
}
|
|
356
|
+
],
|
|
357
|
+
"sourceFile": "safeParseLines.ts",
|
|
358
|
+
"relatedTypes": [
|
|
359
|
+
{
|
|
360
|
+
"name": "ParseLinesOptions",
|
|
361
|
+
"description": "Options for parseLines and safeParseLines.",
|
|
362
|
+
"typeDefinition": "interface ParseLinesOptions {\n blankLines?: \"skip\" | \"fail\";\n invalidLines?: \"skip\" | \"fail\";\n}"
|
|
363
|
+
}
|
|
364
|
+
]
|
|
365
|
+
},
|
|
366
|
+
{
|
|
367
|
+
"name": "safeParseSequence",
|
|
368
|
+
"kind": "function",
|
|
369
|
+
"description": "Parses a JSON text sequence (RFC 7464) like parseSequence, but returns `null` (or\n`fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseSequence does, with the same\nParseSequenceOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
370
|
+
"since": "3.1.3",
|
|
371
|
+
"signatures": [
|
|
372
|
+
{
|
|
373
|
+
"signature": "safeParseSequence<T = unknown>(text: string): T[] | null",
|
|
374
|
+
"description": "Parses a JSON text sequence (RFC 7464) like parseSequence, but returns `null` (or\n`fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseSequence does, with the same\nParseSequenceOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
375
|
+
"params": [
|
|
376
|
+
{
|
|
377
|
+
"name": "text",
|
|
378
|
+
"type": "string",
|
|
379
|
+
"description": "The JSON text sequence"
|
|
380
|
+
}
|
|
381
|
+
],
|
|
382
|
+
"returns": {
|
|
383
|
+
"type": "T[] | null",
|
|
384
|
+
"description": "The parsed values typed as `T[]`, or `fallback` on invalid input"
|
|
385
|
+
},
|
|
386
|
+
"typeParameters": [
|
|
387
|
+
{
|
|
388
|
+
"name": "T",
|
|
389
|
+
"default": "unknown"
|
|
390
|
+
}
|
|
391
|
+
]
|
|
392
|
+
},
|
|
393
|
+
{
|
|
394
|
+
"signature": "safeParseSequence<T = unknown, F = T[]>(text: string, fallback: F, options?: ParseSequenceOptions): F | T[]",
|
|
395
|
+
"description": "Parses a JSON text sequence (RFC 7464) like parseSequence, but returns `null` (or\n`fallback`) instead of throwing on invalid input.\n\n**Accepts / rejects:** exactly what parseSequence does, with the same\nParseSequenceOptions.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws. To pass `options` without\na fallback, pass `null` as the fallback.",
|
|
396
|
+
"params": [
|
|
397
|
+
{
|
|
398
|
+
"name": "text",
|
|
399
|
+
"type": "string",
|
|
400
|
+
"description": "The JSON text sequence"
|
|
401
|
+
},
|
|
402
|
+
{
|
|
403
|
+
"name": "fallback",
|
|
404
|
+
"type": "F",
|
|
405
|
+
"description": "Value returned on invalid input. Defaults to `null` when omitted."
|
|
406
|
+
},
|
|
407
|
+
{
|
|
408
|
+
"name": "options",
|
|
409
|
+
"type": "ParseSequenceOptions",
|
|
410
|
+
"description": "How to handle invalid records — see ParseSequenceOptions",
|
|
411
|
+
"optional": true
|
|
412
|
+
}
|
|
413
|
+
],
|
|
414
|
+
"returns": {
|
|
415
|
+
"type": "F | T[]",
|
|
416
|
+
"description": "The parsed values typed as `T[]`, or `fallback` on invalid input"
|
|
417
|
+
},
|
|
418
|
+
"typeParameters": [
|
|
419
|
+
{
|
|
420
|
+
"name": "T",
|
|
421
|
+
"default": "unknown"
|
|
422
|
+
},
|
|
423
|
+
{
|
|
424
|
+
"name": "F",
|
|
425
|
+
"default": "T[]"
|
|
426
|
+
}
|
|
427
|
+
]
|
|
428
|
+
}
|
|
429
|
+
],
|
|
430
|
+
"examples": [
|
|
431
|
+
{
|
|
432
|
+
"title": "Read a sequence without throwing",
|
|
433
|
+
"description": "Returns null if any record is invalid.",
|
|
434
|
+
"code": "safeParseSequence('\\u001E1\\n') // => [1]\nsafeParseSequence('\\u001E{\\n') // => null"
|
|
435
|
+
},
|
|
436
|
+
{
|
|
437
|
+
"title": "Provide a fallback",
|
|
438
|
+
"description": "The fallback is returned on invalid input.",
|
|
439
|
+
"code": "safeParseSequence('\\u001E{\\n', [])\n// => []"
|
|
440
|
+
}
|
|
441
|
+
],
|
|
442
|
+
"sourceFile": "safeParseSequence.ts",
|
|
443
|
+
"relatedTypes": [
|
|
444
|
+
{
|
|
445
|
+
"name": "ParseSequenceOptions",
|
|
446
|
+
"description": "Options for parseSequence and safeParseSequence.",
|
|
447
|
+
"typeDefinition": "interface ParseSequenceOptions {\n invalidRecords?: \"skip\" | \"fail\";\n}"
|
|
448
|
+
}
|
|
449
|
+
]
|
|
450
|
+
},
|
|
451
|
+
{
|
|
452
|
+
"name": "safeParseStrict",
|
|
453
|
+
"kind": "function",
|
|
454
|
+
"description": "Parses strict JSON like parseStrict, but returns `null` (or `fallback`) instead of\nthrowing on invalid input.\n\n**Accepts / rejects:** exactly what parseStrict does — standard JSON only, no comments,\nno trailing commas, no JSON5 syntax, no leading byte order mark.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
455
|
+
"since": "3.1.3",
|
|
456
|
+
"signatures": [
|
|
457
|
+
{
|
|
458
|
+
"signature": "safeParseStrict<T = unknown>(text: string): T | null",
|
|
459
|
+
"description": "Parses strict JSON like parseStrict, but returns `null` (or `fallback`) instead of\nthrowing on invalid input.\n\n**Accepts / rejects:** exactly what parseStrict does — standard JSON only, no comments,\nno trailing commas, no JSON5 syntax, no leading byte order mark.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
460
|
+
"params": [
|
|
461
|
+
{
|
|
462
|
+
"name": "text",
|
|
463
|
+
"type": "string",
|
|
464
|
+
"description": "The JSON text to parse"
|
|
465
|
+
}
|
|
466
|
+
],
|
|
467
|
+
"returns": {
|
|
468
|
+
"type": "T | null",
|
|
469
|
+
"description": "The parsed value typed as `T`, or `fallback` on invalid input"
|
|
470
|
+
},
|
|
471
|
+
"typeParameters": [
|
|
472
|
+
{
|
|
473
|
+
"name": "T",
|
|
474
|
+
"default": "unknown"
|
|
475
|
+
}
|
|
476
|
+
]
|
|
477
|
+
},
|
|
478
|
+
{
|
|
479
|
+
"signature": "safeParseStrict<T = unknown, F = T>(text: string, fallback: F): T | F",
|
|
480
|
+
"description": "Parses strict JSON like parseStrict, but returns `null` (or `fallback`) instead of\nthrowing on invalid input.\n\n**Accepts / rejects:** exactly what parseStrict does — standard JSON only, no comments,\nno trailing commas, no JSON5 syntax, no leading byte order mark.\n\n**On invalid input:** returns `fallback` when one is given — even `undefined`, which is told\napart from an omitted argument — and `null` otherwise. Never throws.",
|
|
481
|
+
"params": [
|
|
482
|
+
{
|
|
483
|
+
"name": "text",
|
|
484
|
+
"type": "string",
|
|
485
|
+
"description": "The JSON text to parse"
|
|
486
|
+
},
|
|
487
|
+
{
|
|
488
|
+
"name": "fallback",
|
|
489
|
+
"type": "F",
|
|
490
|
+
"description": "Value returned on invalid input. Defaults to `null` when omitted."
|
|
491
|
+
}
|
|
492
|
+
],
|
|
493
|
+
"returns": {
|
|
494
|
+
"type": "T | F",
|
|
495
|
+
"description": "The parsed value typed as `T`, or `fallback` on invalid input"
|
|
496
|
+
},
|
|
497
|
+
"typeParameters": [
|
|
498
|
+
{
|
|
499
|
+
"name": "T",
|
|
500
|
+
"default": "unknown"
|
|
501
|
+
},
|
|
502
|
+
{
|
|
503
|
+
"name": "F",
|
|
504
|
+
"default": "T"
|
|
505
|
+
}
|
|
506
|
+
]
|
|
507
|
+
}
|
|
508
|
+
],
|
|
509
|
+
"examples": [
|
|
510
|
+
{
|
|
511
|
+
"title": "Parse without throwing",
|
|
512
|
+
"description": "Returns null instead of throwing on invalid input.",
|
|
513
|
+
"code": "safeParseStrict('{\"id\":1}') // => { id: 1 }\nsafeParseStrict('{\"id\":1,}') // => null"
|
|
514
|
+
},
|
|
515
|
+
{
|
|
516
|
+
"title": "Provide a fallback",
|
|
517
|
+
"description": "The fallback is returned on invalid input, so the result is never null.",
|
|
518
|
+
"code": "safeParseStrict('oops', { id: 0 })\n// => { id: 0 }"
|
|
519
|
+
}
|
|
520
|
+
],
|
|
521
|
+
"sourceFile": "safeParseStrict.ts"
|
|
522
|
+
},
|
|
523
|
+
{
|
|
524
|
+
"name": "stringify",
|
|
525
|
+
"kind": "function",
|
|
526
|
+
"description": "Serializes a value to JSON text — pretty-printed with 2 spaces by default, or canonical.\n\n**Formatting:**\n- default — indented with 2 spaces, like a hand-written config file\n- `{ indent: n }` — indented with `n` spaces (an integer from 0 to 10); `{ indent: 0 }` gives a\n compact single line, the same as `JSON.stringify(value)`\n- `'canonical'` — canonical JSON (RFC 8785, JSON Canonicalization Scheme): object keys sorted\n by UTF-16 code units, no whitespace at all, numbers in their shortest ECMAScript form. Two\n equal values always give the same bytes, so the output can be hashed, signed or compared.\n Canonical output can't be indented — the RFC fixes every byte.\n\n**Same as `JSON.stringify`:** `toJSON()` is called (a `Date` becomes its ISO string),\n`undefined`, functions and symbols are dropped from objects and become `null` in arrays, and\n`Number`/`String`/`Boolean` objects are unwrapped. Outside `'canonical'`, `NaN` and `±Infinity`\nsilently become `null`, as there.\n\n**Throws** instead of returning `undefined` or dropping data silently:\n- a `TypeError` when `value` itself is `undefined`, a function or a symbol\n- a `TypeError` on a circular structure or a `BigInt` (like `JSON.stringify`)\n- with `'canonical'`, a `RangeError` on `NaN`, `±Infinity` or a string with a lone surrogate,\n which canonical JSON can't hold\n- a `RangeError` when `indent` isn't an integer from 0 to 10\n\nThe output is strict JSON, so it is also valid JSONC and JSON5.",
|
|
527
|
+
"since": "3.1.3",
|
|
528
|
+
"signatures": [
|
|
529
|
+
{
|
|
530
|
+
"signature": "stringify(value: unknown, formatting: StringifyFormatting): string",
|
|
531
|
+
"description": "Serializes a value to JSON text — pretty-printed with 2 spaces by default, or canonical.\n\n**Formatting:**\n- default — indented with 2 spaces, like a hand-written config file\n- `{ indent: n }` — indented with `n` spaces (an integer from 0 to 10); `{ indent: 0 }` gives a\n compact single line, the same as `JSON.stringify(value)`\n- `'canonical'` — canonical JSON (RFC 8785, JSON Canonicalization Scheme): object keys sorted\n by UTF-16 code units, no whitespace at all, numbers in their shortest ECMAScript form. Two\n equal values always give the same bytes, so the output can be hashed, signed or compared.\n Canonical output can't be indented — the RFC fixes every byte.\n\n**Same as `JSON.stringify`:** `toJSON()` is called (a `Date` becomes its ISO string),\n`undefined`, functions and symbols are dropped from objects and become `null` in arrays, and\n`Number`/`String`/`Boolean` objects are unwrapped. Outside `'canonical'`, `NaN` and `±Infinity`\nsilently become `null`, as there.\n\n**Throws** instead of returning `undefined` or dropping data silently:\n- a `TypeError` when `value` itself is `undefined`, a function or a symbol\n- a `TypeError` on a circular structure or a `BigInt` (like `JSON.stringify`)\n- with `'canonical'`, a `RangeError` on `NaN`, `±Infinity` or a string with a lone surrogate,\n which canonical JSON can't hold\n- a `RangeError` when `indent` isn't an integer from 0 to 10\n\nThe output is strict JSON, so it is also valid JSONC and JSON5.",
|
|
532
|
+
"params": [
|
|
533
|
+
{
|
|
534
|
+
"name": "value",
|
|
535
|
+
"type": "unknown",
|
|
536
|
+
"description": "The value to serialize"
|
|
537
|
+
},
|
|
538
|
+
{
|
|
539
|
+
"name": "formatting",
|
|
540
|
+
"type": "StringifyFormatting",
|
|
541
|
+
"description": "`{ indent }` (default `{ indent: 2 }`) or `'canonical'` — see StringifyFormatting",
|
|
542
|
+
"defaultValue": "{}"
|
|
543
|
+
}
|
|
544
|
+
],
|
|
545
|
+
"returns": {
|
|
546
|
+
"type": "string",
|
|
547
|
+
"description": "The JSON text"
|
|
548
|
+
}
|
|
549
|
+
}
|
|
550
|
+
],
|
|
551
|
+
"examples": [
|
|
552
|
+
{
|
|
553
|
+
"title": "Pretty-print by default",
|
|
554
|
+
"description": "Indented with 2 spaces, like a hand-written config file.",
|
|
555
|
+
"code": "stringify({ name: 'app', tags: ['a'] })\n// => '{\\n \"name\": \"app\",\\n \"tags\": [\\n \"a\"\\n ]\\n}'"
|
|
556
|
+
},
|
|
557
|
+
{
|
|
558
|
+
"title": "Compact output",
|
|
559
|
+
"description": "indent: 0 gives a single line, like JSON.stringify(value).",
|
|
560
|
+
"code": "stringify({ b: 1, a: 2 }, { indent: 0 })\n// => '{\"b\":1,\"a\":2}'"
|
|
561
|
+
},
|
|
562
|
+
{
|
|
563
|
+
"title": "Canonical JSON (RFC 8785)",
|
|
564
|
+
"description": "Sorted keys and no whitespace: equal values always give the same bytes, ready to hash or sign.",
|
|
565
|
+
"code": "stringify({ b: 1, a: 2 }, 'canonical')\n// => '{\"a\":2,\"b\":1}'"
|
|
566
|
+
}
|
|
567
|
+
],
|
|
568
|
+
"sourceFile": "stringify.ts",
|
|
569
|
+
"relatedTypes": [
|
|
570
|
+
{
|
|
571
|
+
"name": "StringifyFormatting",
|
|
572
|
+
"description": "How stringify lays out its output:\n- `{ indent }` — pretty-printed with `indent` spaces per level (`0` for a compact single line)\n- `'canonical'` — canonical JSON (RFC 8785), byte-for-byte reproducible",
|
|
573
|
+
"typeDefinition": "type StringifyFormatting = 'canonical' | { indent?: number }"
|
|
574
|
+
}
|
|
575
|
+
]
|
|
576
|
+
},
|
|
577
|
+
{
|
|
578
|
+
"name": "stringifyLines",
|
|
579
|
+
"kind": "function",
|
|
580
|
+
"description": "Serializes values as newline-delimited JSON — one compact JSON value per line, each followed\nby `\\n`. The output is both NDJSON and JSON Lines (`.jsonl`), which are the same format.\n\n**Formatting:**\n- default — each value compact, like `JSON.stringify(value)`\n- `'canonical'` — each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes, for\n hashing or diffing logs line by line\n\nIndentation isn't available: a line break inside a value would split it across lines. String\nvalues can't break a line either — `JSON.stringify` escapes `\\n` and `\\r` inside them.\n\n**Output:** every value is followed by `\\n`, including the last one (NDJSON requires it, and\nJSON Lines recommends it, so files can be concatenated). An empty array gives `''`.\n\n**Throws** a `TypeError` when `values` isn't an array, or when one of them has no JSON\nrepresentation (`undefined`, a function, a symbol, a `BigInt`, a circular structure) — rather\nthan writing a line that would read back as something else. With `'canonical'`, also a\n`RangeError` on `NaN`, `±Infinity` or a lone surrogate (see stringify).",
|
|
581
|
+
"since": "3.1.3",
|
|
582
|
+
"signatures": [
|
|
583
|
+
{
|
|
584
|
+
"signature": "stringifyLines(values: readonly unknown[], formatting?: \"canonical\"): string",
|
|
585
|
+
"description": "Serializes values as newline-delimited JSON — one compact JSON value per line, each followed\nby `\\n`. The output is both NDJSON and JSON Lines (`.jsonl`), which are the same format.\n\n**Formatting:**\n- default — each value compact, like `JSON.stringify(value)`\n- `'canonical'` — each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes, for\n hashing or diffing logs line by line\n\nIndentation isn't available: a line break inside a value would split it across lines. String\nvalues can't break a line either — `JSON.stringify` escapes `\\n` and `\\r` inside them.\n\n**Output:** every value is followed by `\\n`, including the last one (NDJSON requires it, and\nJSON Lines recommends it, so files can be concatenated). An empty array gives `''`.\n\n**Throws** a `TypeError` when `values` isn't an array, or when one of them has no JSON\nrepresentation (`undefined`, a function, a symbol, a `BigInt`, a circular structure) — rather\nthan writing a line that would read back as something else. With `'canonical'`, also a\n`RangeError` on `NaN`, `±Infinity` or a lone surrogate (see stringify).",
|
|
586
|
+
"params": [
|
|
587
|
+
{
|
|
588
|
+
"name": "values",
|
|
589
|
+
"type": "readonly unknown[]",
|
|
590
|
+
"description": "The values to write, one per line"
|
|
591
|
+
},
|
|
592
|
+
{
|
|
593
|
+
"name": "formatting",
|
|
594
|
+
"type": "\"canonical\"",
|
|
595
|
+
"description": "`'canonical'` for canonical JSON lines; compact JSON by default",
|
|
596
|
+
"optional": true
|
|
597
|
+
}
|
|
598
|
+
],
|
|
599
|
+
"returns": {
|
|
600
|
+
"type": "string",
|
|
601
|
+
"description": "The newline-delimited JSON text"
|
|
602
|
+
}
|
|
603
|
+
}
|
|
604
|
+
],
|
|
605
|
+
"examples": [
|
|
606
|
+
{
|
|
607
|
+
"title": "Write NDJSON / JSON Lines",
|
|
608
|
+
"description": "One compact value per line, each followed by a line break.",
|
|
609
|
+
"code": "stringifyLines([{ id: 1 }, { id: 2 }])\n// => '{\"id\":1}\\n{\"id\":2}\\n'"
|
|
610
|
+
},
|
|
611
|
+
{
|
|
612
|
+
"title": "Canonical lines",
|
|
613
|
+
"description": "Each line in canonical form, so a log can be hashed or diffed line by line.",
|
|
614
|
+
"code": "stringifyLines([{ b: 1, a: 2 }], 'canonical')\n// => '{\"a\":2,\"b\":1}\\n'"
|
|
615
|
+
}
|
|
616
|
+
],
|
|
617
|
+
"sourceFile": "stringifyLines.ts"
|
|
618
|
+
},
|
|
619
|
+
{
|
|
620
|
+
"name": "stringifySequence",
|
|
621
|
+
"kind": "function",
|
|
622
|
+
"description": "Serializes values as a JSON text sequence (RFC 7464, `application/json-seq`): each value is\nwritten as the ASCII record separator (RS, U+001E), the compact JSON text, then `\\n` — the\nexact framing the RFC requires of encoders, as `jq --seq` writes it.\n\n**Formatting:**\n- default — each value compact, like `JSON.stringify(value)`\n- `'canonical'` — each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes\n\nIndentation isn't offered, for symmetry with stringifyLines: the trailing `\\n` is what\nlets a reader detect a truncated top-level number, `true`, `false` or `null` (RFC 7464 §2.4).\n\n**Output:** an empty array gives `''`.\n\n**Throws** a `TypeError` when `values` isn't an array, or when one of them has no JSON\nrepresentation (`undefined`, a function, a symbol, a `BigInt`, a circular structure). With\n`'canonical'`, also a `RangeError` on `NaN`, `±Infinity` or a lone surrogate (see\nstringify).",
|
|
623
|
+
"since": "3.1.3",
|
|
624
|
+
"signatures": [
|
|
625
|
+
{
|
|
626
|
+
"signature": "stringifySequence(values: readonly unknown[], formatting?: \"canonical\"): string",
|
|
627
|
+
"description": "Serializes values as a JSON text sequence (RFC 7464, `application/json-seq`): each value is\nwritten as the ASCII record separator (RS, U+001E), the compact JSON text, then `\\n` — the\nexact framing the RFC requires of encoders, as `jq --seq` writes it.\n\n**Formatting:**\n- default — each value compact, like `JSON.stringify(value)`\n- `'canonical'` — each value as canonical JSON (RFC 8785): sorted keys, reproducible bytes\n\nIndentation isn't offered, for symmetry with stringifyLines: the trailing `\\n` is what\nlets a reader detect a truncated top-level number, `true`, `false` or `null` (RFC 7464 §2.4).\n\n**Output:** an empty array gives `''`.\n\n**Throws** a `TypeError` when `values` isn't an array, or when one of them has no JSON\nrepresentation (`undefined`, a function, a symbol, a `BigInt`, a circular structure). With\n`'canonical'`, also a `RangeError` on `NaN`, `±Infinity` or a lone surrogate (see\nstringify).",
|
|
628
|
+
"params": [
|
|
629
|
+
{
|
|
630
|
+
"name": "values",
|
|
631
|
+
"type": "readonly unknown[]",
|
|
632
|
+
"description": "The values to write, one per record"
|
|
633
|
+
},
|
|
634
|
+
{
|
|
635
|
+
"name": "formatting",
|
|
636
|
+
"type": "\"canonical\"",
|
|
637
|
+
"description": "`'canonical'` for canonical JSON records; compact JSON by default",
|
|
638
|
+
"optional": true
|
|
639
|
+
}
|
|
640
|
+
],
|
|
641
|
+
"returns": {
|
|
642
|
+
"type": "string",
|
|
643
|
+
"description": "The JSON text sequence"
|
|
644
|
+
}
|
|
645
|
+
}
|
|
646
|
+
],
|
|
647
|
+
"examples": [
|
|
648
|
+
{
|
|
649
|
+
"title": "Write a JSON text sequence",
|
|
650
|
+
"description": "Each value is framed as RS, JSON text, line feed (RFC 7464).",
|
|
651
|
+
"code": "stringifySequence([{ id: 1 }, 2])\n// => '\\u001E{\"id\":1}\\n\\u001E2\\n'"
|
|
652
|
+
},
|
|
653
|
+
{
|
|
654
|
+
"title": "Canonical records",
|
|
655
|
+
"description": "Each record in canonical form.",
|
|
656
|
+
"code": "stringifySequence([{ b: 1, a: 2 }], 'canonical')\n// => '\\u001E{\"a\":2,\"b\":1}\\n'"
|
|
657
|
+
}
|
|
658
|
+
],
|
|
659
|
+
"sourceFile": "stringifySequence.ts"
|
|
660
|
+
}
|
|
661
|
+
]
|
|
662
|
+
}
|