json-stream-lite 1.3.1 → 1.3.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/README.md +53 -0
- package/dist/byte-buffer.d.ts +11 -0
- package/dist/byte-buffer.js +51 -3
- package/dist/parser.d.ts +147 -0
- package/dist/parser.js +509 -8
- package/dist/stringify.d.ts +26 -0
- package/dist/stringify.js +156 -45
- package/dist/utils.js +2 -1
- package/package.json +11 -7
package/dist/stringify.js
CHANGED
|
@@ -58,24 +58,66 @@ function* formatString(str, chunkSize = 1024) {
|
|
|
58
58
|
i += chunkSize;
|
|
59
59
|
}
|
|
60
60
|
}
|
|
61
|
+
function createContext(replacer, options) {
|
|
62
|
+
if (typeof replacer === 'function') {
|
|
63
|
+
return {
|
|
64
|
+
replacerFn: replacer,
|
|
65
|
+
options,
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
if (Array.isArray(replacer)) {
|
|
69
|
+
// Strings and numbers (or their wrapper objects) name properties
|
|
70
|
+
const propertyList = [];
|
|
71
|
+
for (const item of replacer) {
|
|
72
|
+
if (typeof item === 'string' ||
|
|
73
|
+
typeof item === 'number' ||
|
|
74
|
+
item instanceof String ||
|
|
75
|
+
item instanceof Number) {
|
|
76
|
+
const key = String(item);
|
|
77
|
+
if (!propertyList.includes(key))
|
|
78
|
+
propertyList.push(key);
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
return { propertyList, options };
|
|
82
|
+
}
|
|
83
|
+
return { options };
|
|
84
|
+
}
|
|
61
85
|
/**
|
|
62
|
-
*
|
|
63
|
-
*
|
|
86
|
+
* Whether a value has no JSON representation (undefined, functions and
|
|
87
|
+
* symbols). Such values are left out of objects and written as `null` in
|
|
88
|
+
* arrays.
|
|
64
89
|
*
|
|
65
90
|
* @param value - The value to check
|
|
66
|
-
* @returns True if the value
|
|
91
|
+
* @returns True if the value has no JSON representation
|
|
67
92
|
*/
|
|
68
|
-
function
|
|
69
|
-
return
|
|
93
|
+
function hasNoJsonValue(value) {
|
|
94
|
+
return (value === undefined ||
|
|
95
|
+
typeof value === 'function' ||
|
|
96
|
+
typeof value === 'symbol');
|
|
70
97
|
}
|
|
71
98
|
/**
|
|
72
|
-
*
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
99
|
+
* Works out the value to write for `holder[key]`, in the same order as
|
|
100
|
+
* `JSON.stringify`: `toJSON(key)` first, then the replacer function (called
|
|
101
|
+
* with `holder` as `this`). An object with its own `toString()` is then
|
|
102
|
+
* written as that string.
|
|
76
103
|
*/
|
|
77
|
-
function
|
|
78
|
-
|
|
104
|
+
function resolveValue(holder, key, value, context) {
|
|
105
|
+
if ((typeof value === 'object' && value !== null) ||
|
|
106
|
+
typeof value === 'bigint') {
|
|
107
|
+
const toJSON = value.toJSON;
|
|
108
|
+
if (typeof toJSON === 'function') {
|
|
109
|
+
value = toJSON.call(value, key);
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
if (context.replacerFn) {
|
|
113
|
+
value = context.replacerFn.call(holder, key, value);
|
|
114
|
+
}
|
|
115
|
+
if (value &&
|
|
116
|
+
typeof value === 'object' &&
|
|
117
|
+
hasCustomToStringFunction(value)) {
|
|
118
|
+
value = value.toString();
|
|
119
|
+
}
|
|
120
|
+
return value;
|
|
79
121
|
}
|
|
80
122
|
/**
|
|
81
123
|
* Type guard to check if an object has a custom toString method.
|
|
@@ -110,30 +152,19 @@ function* emitComments(path, options, indentStr) {
|
|
|
110
152
|
}
|
|
111
153
|
}
|
|
112
154
|
/**
|
|
113
|
-
* Internal recursive function that stringifies
|
|
114
|
-
*
|
|
155
|
+
* Internal recursive function that stringifies an already resolved value
|
|
156
|
+
* (see {@link resolveValue}) with depth tracking.
|
|
115
157
|
*
|
|
116
158
|
* @param value - The value to stringify
|
|
117
|
-
* @param
|
|
159
|
+
* @param context - Replacer and options
|
|
118
160
|
* @param indent - Number of spaces for indentation (0 for compact)
|
|
119
161
|
* @param currentDepth - Current nesting depth for indentation
|
|
120
162
|
* @param start - Indicates if this is the start of the stringification
|
|
121
|
-
* @param options - Stringification options
|
|
122
163
|
* @param currentPath - Current dot-notation path for comment matching
|
|
123
164
|
* @yields String chunks representing the JSON output
|
|
124
165
|
*/
|
|
125
|
-
function*
|
|
126
|
-
|
|
127
|
-
value = replacer('', value);
|
|
128
|
-
}
|
|
129
|
-
if (value && typeof value === 'object') {
|
|
130
|
-
if (hasCustomToJsonFunction(value)) {
|
|
131
|
-
value = value.toJSON();
|
|
132
|
-
}
|
|
133
|
-
else if (hasCustomToStringFunction(value)) {
|
|
134
|
-
value = value.toString();
|
|
135
|
-
}
|
|
136
|
-
}
|
|
166
|
+
function* stringifyValue(value, context, indent = 0, currentDepth = 0, start = true, currentPath = '') {
|
|
167
|
+
const options = context.options;
|
|
137
168
|
// If this is the first call and indenting, add initial indentation
|
|
138
169
|
if (start && indent > 0 && currentDepth > 0) {
|
|
139
170
|
yield ' '.repeat(indent * currentDepth);
|
|
@@ -150,6 +181,10 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
150
181
|
else if (typeof value === 'string') {
|
|
151
182
|
yield* formatString(value, options?.stringChunkSize);
|
|
152
183
|
}
|
|
184
|
+
else if (typeof value === 'bigint') {
|
|
185
|
+
// As JSON.stringify: JSON has no BigInt type
|
|
186
|
+
throw new TypeError('Do not know how to serialize a BigInt');
|
|
187
|
+
}
|
|
153
188
|
else if (Array.isArray(value)) {
|
|
154
189
|
const len = value.length;
|
|
155
190
|
const valueIndent = indent * (currentDepth + 1);
|
|
@@ -157,7 +192,7 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
157
192
|
const isJsonc = options?.jsonc === true;
|
|
158
193
|
yield '[';
|
|
159
194
|
for (let i = 0; i < len; i++) {
|
|
160
|
-
const next = value[i];
|
|
195
|
+
const next = resolveValue(value, String(i), value[i], context);
|
|
161
196
|
const isLast = i === len - 1;
|
|
162
197
|
if (i > 0) {
|
|
163
198
|
yield ',';
|
|
@@ -168,14 +203,14 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
168
203
|
yield* emitComments(itemPath, options, valueIndentStr);
|
|
169
204
|
yield valueIndentStr;
|
|
170
205
|
}
|
|
171
|
-
if (
|
|
206
|
+
if (hasNoJsonValue(next)) {
|
|
172
207
|
yield 'null';
|
|
173
208
|
if (isLast && isJsonc)
|
|
174
209
|
yield ',';
|
|
175
210
|
continue;
|
|
176
211
|
}
|
|
177
212
|
const itemPath = currentPath ? `${currentPath}[${i}]` : `[${i}]`;
|
|
178
|
-
yield*
|
|
213
|
+
yield* stringifyValue(next, context, indent > 0 ? indent : 0, currentDepth + 1, false, itemPath);
|
|
179
214
|
if (isLast && isJsonc)
|
|
180
215
|
yield ',';
|
|
181
216
|
}
|
|
@@ -185,20 +220,18 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
185
220
|
yield ']';
|
|
186
221
|
}
|
|
187
222
|
else if (typeof value === 'object') {
|
|
188
|
-
const
|
|
189
|
-
const
|
|
223
|
+
const object = value;
|
|
224
|
+
const keys = context.propertyList ?? Object.keys(object);
|
|
190
225
|
const valueIndent = indent * (currentDepth + 1);
|
|
191
226
|
const valueIndentStr = indent > 0 ? ' '.repeat(valueIndent) : '';
|
|
192
227
|
const isJsonc = options?.jsonc === true;
|
|
193
228
|
let count = 0;
|
|
194
|
-
// Count non-ignored keys for trailing comma logic
|
|
195
|
-
const validKeys = keys.filter((k) => !shouldIgnore(value[k]));
|
|
196
|
-
const totalValid = validKeys.length;
|
|
197
229
|
yield '{';
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
230
|
+
// Members are resolved one at a time, in order, so a replacer is
|
|
231
|
+
// called depth-first as with JSON.stringify.
|
|
232
|
+
for (const key of keys) {
|
|
233
|
+
const next = resolveValue(object, key, object[key], context);
|
|
234
|
+
if (hasNoJsonValue(next)) {
|
|
202
235
|
continue;
|
|
203
236
|
}
|
|
204
237
|
if (count > 0) {
|
|
@@ -212,12 +245,13 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
212
245
|
}
|
|
213
246
|
yield* formatString(key, options?.stringChunkSize);
|
|
214
247
|
yield `:${indent > 0 ? ' ' : ''}`;
|
|
215
|
-
yield*
|
|
248
|
+
yield* stringifyValue(next, context, indent > 0 ? indent : 0, currentDepth + 1, false, memberPath);
|
|
216
249
|
count++;
|
|
217
|
-
if (count === totalValid && isJsonc)
|
|
218
|
-
yield ',';
|
|
219
250
|
}
|
|
220
|
-
|
|
251
|
+
// JSONC: trailing comma after the last member that was written
|
|
252
|
+
if (count > 0 && isJsonc)
|
|
253
|
+
yield ',';
|
|
254
|
+
if (indent > 0 && count > 0) {
|
|
221
255
|
yield '\n' + ' '.repeat(indent * currentDepth);
|
|
222
256
|
}
|
|
223
257
|
yield '}';
|
|
@@ -242,7 +276,16 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
242
276
|
* ```
|
|
243
277
|
*/
|
|
244
278
|
export function jsonStreamStringify(value, replacer, indent = 0, options) {
|
|
245
|
-
return
|
|
279
|
+
return jsonStreamStringifyRoot(value, replacer, indent, options);
|
|
280
|
+
}
|
|
281
|
+
function* jsonStreamStringifyRoot(value, replacer, indent, options) {
|
|
282
|
+
const context = createContext(replacer, options);
|
|
283
|
+
// As JSON.stringify, the root is resolved with key '' on a wrapper object
|
|
284
|
+
const resolved = resolveValue({ '': value }, '', value, context);
|
|
285
|
+
// A root with no JSON representation (e.g. undefined) produces no output
|
|
286
|
+
if (hasNoJsonValue(resolved))
|
|
287
|
+
return;
|
|
288
|
+
yield* stringifyValue(resolved, context, indent, 0, true);
|
|
246
289
|
}
|
|
247
290
|
/**
|
|
248
291
|
* Converts a JavaScript value to JSON format, yielding Uint8Array byte chunks.
|
|
@@ -269,12 +312,23 @@ export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options)
|
|
|
269
312
|
}
|
|
270
313
|
}
|
|
271
314
|
export async function* jsonStreamStringifyAsync(value, replacer, indent = 0, options) {
|
|
315
|
+
const context = createContext(replacer, options);
|
|
272
316
|
yield '[';
|
|
273
317
|
if (indent > 0)
|
|
274
318
|
yield '\n';
|
|
275
319
|
let isFirst = true;
|
|
320
|
+
let index = 0;
|
|
276
321
|
for await (const item of value) {
|
|
277
|
-
|
|
322
|
+
// Each item is resolved as an array element, with its index as key.
|
|
323
|
+
// There is no array to pass as the replacer's `this`.
|
|
324
|
+
const resolved = resolveValue(undefined, String(index++), item, context);
|
|
325
|
+
const stringGenerator = hasNoJsonValue(resolved)
|
|
326
|
+
? (function* () {
|
|
327
|
+
if (indent > 0)
|
|
328
|
+
yield ' '.repeat(indent);
|
|
329
|
+
yield 'null';
|
|
330
|
+
})()
|
|
331
|
+
: stringifyValue(resolved, context, indent, 1, true);
|
|
278
332
|
if (!isFirst) {
|
|
279
333
|
yield ',';
|
|
280
334
|
if (indent > 0)
|
|
@@ -296,3 +350,60 @@ export async function* jsonStreamStringifyBytesAsync(value, replacer, indent = 0
|
|
|
296
350
|
yield stringToBytes(chunk);
|
|
297
351
|
}
|
|
298
352
|
}
|
|
353
|
+
/**
|
|
354
|
+
* Serializes a single JSON Lines record. Values with no JSON representation
|
|
355
|
+
* (undefined, functions) are written as `null` so every input value produces
|
|
356
|
+
* exactly one line.
|
|
357
|
+
*/
|
|
358
|
+
function* jsonLinesStringifyLine(value, replacer, options) {
|
|
359
|
+
let empty = true;
|
|
360
|
+
// Every line must be a plain JSON value, so JSONC output is never used.
|
|
361
|
+
const lineOptions = options?.jsonc ? { ...options, jsonc: false } : options;
|
|
362
|
+
for (const chunk of jsonStreamStringify(value, replacer, 0, lineOptions)) {
|
|
363
|
+
empty = false;
|
|
364
|
+
yield chunk;
|
|
365
|
+
}
|
|
366
|
+
yield empty ? 'null\n' : '\n';
|
|
367
|
+
}
|
|
368
|
+
/**
|
|
369
|
+
* Serializes values as JSON Lines (JSONL / NDJSON): one compact JSON value per
|
|
370
|
+
* line, each terminated by a newline. Values with no JSON representation
|
|
371
|
+
* (undefined, functions) are written as `null`. The `jsonc` option is ignored.
|
|
372
|
+
*
|
|
373
|
+
* @param values - The values to serialize, one per line
|
|
374
|
+
* @param replacer - Optional replacer function or array
|
|
375
|
+
* @param options - Additional stringification options
|
|
376
|
+
* @returns Generator yielding string chunks
|
|
377
|
+
*/
|
|
378
|
+
export function* jsonLinesStringify(values, replacer, options) {
|
|
379
|
+
for (const value of values) {
|
|
380
|
+
yield* jsonLinesStringifyLine(value, replacer, options);
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
/**
|
|
384
|
+
* Serializes values as JSON Lines, yielding Uint8Array chunks.
|
|
385
|
+
* See {@link jsonLinesStringify}.
|
|
386
|
+
*/
|
|
387
|
+
export function* jsonLinesStringifyBytes(values, replacer, options) {
|
|
388
|
+
for (const chunk of jsonLinesStringify(values, replacer, options)) {
|
|
389
|
+
yield stringToBytes(chunk);
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
/**
|
|
393
|
+
* Asynchronously serializes values (sync or async iterable) as JSON Lines.
|
|
394
|
+
* See {@link jsonLinesStringify}.
|
|
395
|
+
*/
|
|
396
|
+
export async function* jsonLinesStringifyAsync(values, replacer, options) {
|
|
397
|
+
for await (const value of values) {
|
|
398
|
+
yield* jsonLinesStringifyLine(value, replacer, options);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Asynchronously serializes values as JSON Lines, yielding Uint8Array chunks.
|
|
403
|
+
* See {@link jsonLinesStringify}.
|
|
404
|
+
*/
|
|
405
|
+
export async function* jsonLinesStringifyBytesAsync(values, replacer, options) {
|
|
406
|
+
for await (const chunk of jsonLinesStringifyAsync(values, replacer, options)) {
|
|
407
|
+
yield stringToBytes(chunk);
|
|
408
|
+
}
|
|
409
|
+
}
|
package/dist/utils.js
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
const textEncoder = new TextEncoder();
|
|
2
|
-
|
|
2
|
+
// ignoreBOM keeps a leading U+FEFF in decoded text instead of dropping it.
|
|
3
|
+
const textDecoder = new TextDecoder('utf-8', { ignoreBOM: true });
|
|
3
4
|
/**
|
|
4
5
|
* Converts a string to a Uint8Array of UTF-8 encoded bytes.
|
|
5
6
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.3",
|
|
4
4
|
"description": "A lightweight, memory-efficient, zero-dependency streaming JSON parser and stringifier for JavaScript and TypeScript. It works in both Node.js and browser environments.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -17,9 +17,13 @@
|
|
|
17
17
|
"types": "./dist/index.d.ts",
|
|
18
18
|
"default": "./dist/index.js"
|
|
19
19
|
},
|
|
20
|
+
"./*.js": {
|
|
21
|
+
"types": "./dist/*.d.ts",
|
|
22
|
+
"default": "./dist/*.js"
|
|
23
|
+
},
|
|
20
24
|
"./*": {
|
|
21
25
|
"types": "./dist/*.d.ts",
|
|
22
|
-
"default": "./dist
|
|
26
|
+
"default": "./dist/*.js"
|
|
23
27
|
}
|
|
24
28
|
},
|
|
25
29
|
"keywords": [
|
|
@@ -46,12 +50,12 @@
|
|
|
46
50
|
"author": "Jacob Shirley",
|
|
47
51
|
"license": "MIT",
|
|
48
52
|
"devDependencies": {
|
|
53
|
+
"@vitest/browser": "^5.0.0",
|
|
54
|
+
"@vitest/browser-playwright": "^5.0.0",
|
|
55
|
+
"@vitest/coverage-v8": "^5.0.0",
|
|
56
|
+
"playwright": "^1.65.1",
|
|
49
57
|
"typescript": "^6.0.2",
|
|
50
|
-
"vitest": "^
|
|
51
|
-
"@vitest/browser": "^4.0.14",
|
|
52
|
-
"@vitest/browser-playwright": "^4.0.14",
|
|
53
|
-
"@vitest/coverage-v8": "^4.0.14",
|
|
54
|
-
"playwright": "^1.57.0"
|
|
58
|
+
"vitest": "^5.0.0"
|
|
55
59
|
},
|
|
56
60
|
"files": [
|
|
57
61
|
"dist",
|