json-stream-lite 1.0.0
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 +528 -0
- package/dist/byte-buffer.d.ts +76 -0
- package/dist/byte-buffer.js +175 -0
- package/dist/generators.d.ts +3 -0
- package/dist/generators.js +51 -0
- package/dist/incremental-parser.d.ts +103 -0
- package/dist/incremental-parser.js +184 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4 -0
- package/dist/json-key-value-pair.d.ts +6 -0
- package/dist/json-key-value-pair.js +8 -0
- package/dist/json-parser.d.ts +64 -0
- package/dist/json-parser.js +428 -0
- package/dist/parser.d.ts +67 -0
- package/dist/parser.js +453 -0
- package/dist/stringify.d.ts +6 -0
- package/dist/stringify.js +92 -0
- package/dist/types.d.ts +3 -0
- package/dist/types.js +1 -0
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +10 -0
- package/package.json +39 -0
package/dist/parser.js
ADDED
|
@@ -0,0 +1,453 @@
|
|
|
1
|
+
import { ByteBuffer } from './byte-buffer.js';
|
|
2
|
+
import { bytesToNumber, bytesToString } from './utils.js';
|
|
3
|
+
const BYTE_MAP = {
|
|
4
|
+
quotes: 34,
|
|
5
|
+
a: 97,
|
|
6
|
+
n: 110,
|
|
7
|
+
t: 116,
|
|
8
|
+
f: 102,
|
|
9
|
+
u: 117,
|
|
10
|
+
r: 114,
|
|
11
|
+
l: 108,
|
|
12
|
+
s: 115,
|
|
13
|
+
zero: 48,
|
|
14
|
+
nine: 57,
|
|
15
|
+
minus: 45,
|
|
16
|
+
dot: 46,
|
|
17
|
+
plus: 43,
|
|
18
|
+
e: 101,
|
|
19
|
+
E: 69,
|
|
20
|
+
space: 32,
|
|
21
|
+
tab: 9,
|
|
22
|
+
carriageReturn: 13,
|
|
23
|
+
lineFeed: 10,
|
|
24
|
+
leftBrace: 123,
|
|
25
|
+
rightBrace: 125,
|
|
26
|
+
leftSquare: 91,
|
|
27
|
+
rightSquare: 93,
|
|
28
|
+
comma: 44,
|
|
29
|
+
colon: 58,
|
|
30
|
+
};
|
|
31
|
+
const isWhitespace = (byte) => {
|
|
32
|
+
return (byte === BYTE_MAP.space ||
|
|
33
|
+
byte === BYTE_MAP.tab ||
|
|
34
|
+
byte === BYTE_MAP.carriageReturn ||
|
|
35
|
+
byte === BYTE_MAP.lineFeed);
|
|
36
|
+
};
|
|
37
|
+
const isDigit = (byte) => {
|
|
38
|
+
return byte !== null && byte >= BYTE_MAP.zero && byte <= BYTE_MAP.nine;
|
|
39
|
+
};
|
|
40
|
+
const isNumberStart = (byte) => {
|
|
41
|
+
return (byte === BYTE_MAP.minus ||
|
|
42
|
+
(byte !== null && byte >= BYTE_MAP.zero && byte <= BYTE_MAP.nine));
|
|
43
|
+
};
|
|
44
|
+
const isNumeric = (byte) => {
|
|
45
|
+
return (isDigit(byte) ||
|
|
46
|
+
byte === BYTE_MAP.dot || // .
|
|
47
|
+
byte === BYTE_MAP.e || // e
|
|
48
|
+
byte === BYTE_MAP.E || // E
|
|
49
|
+
byte === BYTE_MAP.minus || // -
|
|
50
|
+
byte === BYTE_MAP.plus // +
|
|
51
|
+
);
|
|
52
|
+
};
|
|
53
|
+
export class JsonEntity {
|
|
54
|
+
consumed = false;
|
|
55
|
+
buffer;
|
|
56
|
+
constructor(buffer) {
|
|
57
|
+
this.buffer =
|
|
58
|
+
buffer instanceof ByteBuffer ? buffer : new ByteBuffer(buffer);
|
|
59
|
+
}
|
|
60
|
+
get entityType() {
|
|
61
|
+
return this.constructor.name;
|
|
62
|
+
}
|
|
63
|
+
set maxBufferSize(size) {
|
|
64
|
+
this.buffer.maxBufferSize = size;
|
|
65
|
+
}
|
|
66
|
+
get bufferLength() {
|
|
67
|
+
return this.buffer.length;
|
|
68
|
+
}
|
|
69
|
+
feed(...input) {
|
|
70
|
+
for (const item of input) {
|
|
71
|
+
this.buffer.feed(item);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
skipWhitespace() {
|
|
75
|
+
while (isWhitespace(this.buffer.peek())) {
|
|
76
|
+
this.buffer.next();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
read() {
|
|
80
|
+
if (this.consumed) {
|
|
81
|
+
throw new Error('JSON entity has already been consumed.');
|
|
82
|
+
}
|
|
83
|
+
const read = this.parse();
|
|
84
|
+
this.consumed = true;
|
|
85
|
+
if (this.buffer.canCompact())
|
|
86
|
+
this.buffer.compact();
|
|
87
|
+
return read;
|
|
88
|
+
}
|
|
89
|
+
async readAsync() {
|
|
90
|
+
if (this.consumed) {
|
|
91
|
+
throw new Error('JSON entity has already been consumed.');
|
|
92
|
+
}
|
|
93
|
+
while (!this.buffer.atEof()) {
|
|
94
|
+
await this.buffer.readStream();
|
|
95
|
+
const res = this.tryParse(() => this.read());
|
|
96
|
+
if (res !== undefined) {
|
|
97
|
+
return res;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
return this.read();
|
|
101
|
+
}
|
|
102
|
+
consume() {
|
|
103
|
+
if (!this.consumed) {
|
|
104
|
+
this.read();
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
async consumeAsync() {
|
|
108
|
+
if (!this.consumed) {
|
|
109
|
+
await this.readAsync();
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
tryParse(cb) {
|
|
113
|
+
if (this.consumed) {
|
|
114
|
+
throw new Error('JSON entity has already been consumed.');
|
|
115
|
+
}
|
|
116
|
+
this.buffer.locked = true;
|
|
117
|
+
return this.buffer.resetOnFail(() => {
|
|
118
|
+
const res = cb(this);
|
|
119
|
+
this.buffer.locked = false;
|
|
120
|
+
return res;
|
|
121
|
+
}, (e) => {
|
|
122
|
+
this.buffer.locked = false;
|
|
123
|
+
});
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
export class JsonString extends JsonEntity {
|
|
127
|
+
parse() {
|
|
128
|
+
const bytes = [];
|
|
129
|
+
this.buffer.next(); // consume opening quotes
|
|
130
|
+
while (this.buffer.peek() !== BYTE_MAP.quotes) {
|
|
131
|
+
bytes.push(this.buffer.next());
|
|
132
|
+
}
|
|
133
|
+
this.buffer.next(); // consume closing quotes
|
|
134
|
+
const result = bytesToString(new Uint8Array(bytes));
|
|
135
|
+
return result;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
export class JsonNumber extends JsonEntity {
|
|
139
|
+
parse() {
|
|
140
|
+
const numberBytes = [];
|
|
141
|
+
while (isNumeric(this.buffer.peek())) {
|
|
142
|
+
numberBytes.push(this.buffer.next());
|
|
143
|
+
}
|
|
144
|
+
const result = bytesToNumber(new Uint8Array(numberBytes));
|
|
145
|
+
return result;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
export class JsonBoolean extends JsonEntity {
|
|
149
|
+
parse() {
|
|
150
|
+
const next = this.buffer.next();
|
|
151
|
+
if (next === BYTE_MAP.t) {
|
|
152
|
+
this.buffer.expect(BYTE_MAP.r); // r
|
|
153
|
+
this.buffer.expect(BYTE_MAP.u); // u
|
|
154
|
+
this.buffer.expect(BYTE_MAP.e); // e
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
this.buffer.expect(BYTE_MAP.a); // a
|
|
159
|
+
this.buffer.expect(BYTE_MAP.l); // l
|
|
160
|
+
this.buffer.expect(BYTE_MAP.s); // s
|
|
161
|
+
this.buffer.expect(BYTE_MAP.e); // e
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
export class JsonNull extends JsonEntity {
|
|
167
|
+
parse() {
|
|
168
|
+
this.buffer.expect(BYTE_MAP.n); // n
|
|
169
|
+
this.buffer.expect(BYTE_MAP.u); // u
|
|
170
|
+
this.buffer.expect(BYTE_MAP.l); // l
|
|
171
|
+
this.buffer.expect(BYTE_MAP.l); // l
|
|
172
|
+
return null;
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
export class JsonValue extends JsonEntity {
|
|
176
|
+
key;
|
|
177
|
+
value;
|
|
178
|
+
constructor(buffer, key) {
|
|
179
|
+
super(buffer);
|
|
180
|
+
this.key = key;
|
|
181
|
+
}
|
|
182
|
+
parse() {
|
|
183
|
+
this.key?.consume();
|
|
184
|
+
this.skipWhitespace();
|
|
185
|
+
if (this.buffer.peek() === BYTE_MAP.colon) {
|
|
186
|
+
this.buffer.next(); // consume :
|
|
187
|
+
this.skipWhitespace();
|
|
188
|
+
}
|
|
189
|
+
if (this.buffer.peek() === BYTE_MAP.comma) {
|
|
190
|
+
this.buffer.next(); // consume ,
|
|
191
|
+
this.skipWhitespace();
|
|
192
|
+
}
|
|
193
|
+
const next = this.buffer.peek();
|
|
194
|
+
if (next === BYTE_MAP.quotes) {
|
|
195
|
+
return new JsonString(this.buffer);
|
|
196
|
+
}
|
|
197
|
+
else if (isNumberStart(next)) {
|
|
198
|
+
return new JsonNumber(this.buffer);
|
|
199
|
+
}
|
|
200
|
+
else if (next === BYTE_MAP.t || next === BYTE_MAP.f) {
|
|
201
|
+
return new JsonBoolean(this.buffer);
|
|
202
|
+
}
|
|
203
|
+
else if (next === BYTE_MAP.n) {
|
|
204
|
+
return new JsonNull(this.buffer);
|
|
205
|
+
}
|
|
206
|
+
else if (next === BYTE_MAP.leftBrace) {
|
|
207
|
+
return new JsonObject(this.buffer);
|
|
208
|
+
}
|
|
209
|
+
else if (next === BYTE_MAP.leftSquare) {
|
|
210
|
+
return new JsonArray(this.buffer);
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
throw new Error('Unexpected token while parsing JSON value: ' +
|
|
214
|
+
String.fromCharCode(next ?? 0));
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
read() {
|
|
218
|
+
if (this.value) {
|
|
219
|
+
return this.value;
|
|
220
|
+
}
|
|
221
|
+
this.value = super.read();
|
|
222
|
+
this.consumed = false;
|
|
223
|
+
return this.value;
|
|
224
|
+
}
|
|
225
|
+
readValue() {
|
|
226
|
+
const value = this.read();
|
|
227
|
+
return value.read();
|
|
228
|
+
}
|
|
229
|
+
async readValueAsync() {
|
|
230
|
+
const value = await this.readAsync();
|
|
231
|
+
return await value.readAsync();
|
|
232
|
+
}
|
|
233
|
+
async readAsync() {
|
|
234
|
+
if (this.value) {
|
|
235
|
+
return this.value;
|
|
236
|
+
}
|
|
237
|
+
this.value = await super.readAsync();
|
|
238
|
+
this.consumed = false;
|
|
239
|
+
return this.value;
|
|
240
|
+
}
|
|
241
|
+
consume() {
|
|
242
|
+
if (this.value && !this.value.consumed) {
|
|
243
|
+
this.value.consume();
|
|
244
|
+
}
|
|
245
|
+
else {
|
|
246
|
+
super.consume();
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
async consumeAsync() {
|
|
250
|
+
if (this.value && !this.value.consumed) {
|
|
251
|
+
await this.value.consumeAsync();
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
await super.consumeAsync();
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
export class JsonObject extends JsonEntity {
|
|
259
|
+
*members() {
|
|
260
|
+
this.skipWhitespace();
|
|
261
|
+
if (this.buffer.peek() === BYTE_MAP.leftBrace) {
|
|
262
|
+
this.buffer.next(); // consume {
|
|
263
|
+
}
|
|
264
|
+
this.skipWhitespace();
|
|
265
|
+
if (this.buffer.peek() === BYTE_MAP.rightBrace) {
|
|
266
|
+
this.buffer.next(); // consume }
|
|
267
|
+
return [];
|
|
268
|
+
}
|
|
269
|
+
while (this.buffer.peek() !== BYTE_MAP.rightBrace) {
|
|
270
|
+
this.skipWhitespace();
|
|
271
|
+
if (this.buffer.peek() === BYTE_MAP.comma) {
|
|
272
|
+
this.buffer.next(); // consume ,
|
|
273
|
+
this.skipWhitespace();
|
|
274
|
+
}
|
|
275
|
+
const key = new JsonString(this.buffer);
|
|
276
|
+
const value = new JsonValue(this.buffer, key);
|
|
277
|
+
yield [key, value];
|
|
278
|
+
key.consume();
|
|
279
|
+
value.consume();
|
|
280
|
+
this.skipWhitespace();
|
|
281
|
+
if (this.buffer.peek() === BYTE_MAP.comma) {
|
|
282
|
+
this.buffer.next(); // consume ,
|
|
283
|
+
this.skipWhitespace();
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
this.buffer.expect(BYTE_MAP.rightBrace);
|
|
287
|
+
this.consumed = true;
|
|
288
|
+
}
|
|
289
|
+
async *membersAsync() {
|
|
290
|
+
while (!this.buffer.atEof()) {
|
|
291
|
+
await this.buffer.readStream();
|
|
292
|
+
const memberGen = this.members();
|
|
293
|
+
let currentMember = undefined;
|
|
294
|
+
while (true) {
|
|
295
|
+
currentMember = this.tryParse(() => memberGen.next());
|
|
296
|
+
if (currentMember === undefined) {
|
|
297
|
+
break; // Need more data
|
|
298
|
+
}
|
|
299
|
+
if (currentMember.done) {
|
|
300
|
+
return; // No more members
|
|
301
|
+
}
|
|
302
|
+
yield currentMember.value;
|
|
303
|
+
// Make sure to consume the key and value asynchronously
|
|
304
|
+
await currentMember.value[0].consumeAsync();
|
|
305
|
+
await currentMember.value[1].consumeAsync();
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
[Symbol.iterator]() {
|
|
310
|
+
return this.members();
|
|
311
|
+
}
|
|
312
|
+
[Symbol.asyncIterator]() {
|
|
313
|
+
return this.membersAsync();
|
|
314
|
+
}
|
|
315
|
+
parse() {
|
|
316
|
+
const obj = {};
|
|
317
|
+
for (const [key, value] of this.members()) {
|
|
318
|
+
obj[key.read()] = value.read().read();
|
|
319
|
+
}
|
|
320
|
+
return obj;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
export class JsonArray extends JsonEntity {
|
|
324
|
+
*items() {
|
|
325
|
+
this.skipWhitespace();
|
|
326
|
+
if (this.buffer.peek() === BYTE_MAP.leftSquare) {
|
|
327
|
+
this.buffer.next(); // consume [
|
|
328
|
+
}
|
|
329
|
+
this.skipWhitespace();
|
|
330
|
+
if (this.buffer.peek() === BYTE_MAP.rightSquare) {
|
|
331
|
+
this.buffer.next(); // consume ]
|
|
332
|
+
return [];
|
|
333
|
+
}
|
|
334
|
+
while (this.buffer.peek() !== BYTE_MAP.rightSquare) {
|
|
335
|
+
this.skipWhitespace();
|
|
336
|
+
if (this.buffer.peek() === BYTE_MAP.comma) {
|
|
337
|
+
this.buffer.next(); // consume ,
|
|
338
|
+
this.skipWhitespace();
|
|
339
|
+
}
|
|
340
|
+
const value = new JsonValue(this.buffer).read();
|
|
341
|
+
yield value;
|
|
342
|
+
if (!value.consumed)
|
|
343
|
+
value.read();
|
|
344
|
+
this.skipWhitespace();
|
|
345
|
+
}
|
|
346
|
+
this.buffer.expect(BYTE_MAP.rightSquare);
|
|
347
|
+
this.consumed = true;
|
|
348
|
+
}
|
|
349
|
+
async *itemsAsync() {
|
|
350
|
+
while (!this.buffer.atEof()) {
|
|
351
|
+
await this.buffer.readStream();
|
|
352
|
+
const itemGen = this.items();
|
|
353
|
+
let currentItem = undefined;
|
|
354
|
+
while (true) {
|
|
355
|
+
currentItem = this.tryParse(() => itemGen.next());
|
|
356
|
+
if (currentItem === undefined) {
|
|
357
|
+
break; // Need more data
|
|
358
|
+
}
|
|
359
|
+
if (currentItem.done) {
|
|
360
|
+
return; // No more items
|
|
361
|
+
}
|
|
362
|
+
yield currentItem.value;
|
|
363
|
+
await currentItem.value.consumeAsync();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
[Symbol.iterator]() {
|
|
368
|
+
return this.items();
|
|
369
|
+
}
|
|
370
|
+
[Symbol.asyncIterator]() {
|
|
371
|
+
return this.itemsAsync();
|
|
372
|
+
}
|
|
373
|
+
parse() {
|
|
374
|
+
const values = [];
|
|
375
|
+
for (const value of this.items()) {
|
|
376
|
+
values.push(value.read());
|
|
377
|
+
}
|
|
378
|
+
return values;
|
|
379
|
+
}
|
|
380
|
+
}
|
|
381
|
+
export class JsonKeyValueParser extends JsonEntity {
|
|
382
|
+
container;
|
|
383
|
+
parentKey;
|
|
384
|
+
constructor(buffer, container, parentKey) {
|
|
385
|
+
super(buffer);
|
|
386
|
+
this.container = container ?? new JsonValue(this.buffer);
|
|
387
|
+
this.parentKey = parentKey;
|
|
388
|
+
}
|
|
389
|
+
*parse() {
|
|
390
|
+
if (this.container instanceof JsonObject) {
|
|
391
|
+
for (const [keyEntity, valueEntity] of this.container) {
|
|
392
|
+
const key = keyEntity.read();
|
|
393
|
+
const value = valueEntity.read();
|
|
394
|
+
const finalKey = this.parentKey
|
|
395
|
+
? `${this.parentKey}.${key}`
|
|
396
|
+
: key;
|
|
397
|
+
if (value instanceof JsonArray || value instanceof JsonObject) {
|
|
398
|
+
yield* new JsonKeyValueParser(this.buffer, value, finalKey).parse();
|
|
399
|
+
}
|
|
400
|
+
else {
|
|
401
|
+
yield [finalKey, value.read()];
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
else if (this.container instanceof JsonArray) {
|
|
406
|
+
let index = 0;
|
|
407
|
+
for (const valueEntity of this.container) {
|
|
408
|
+
const value = valueEntity;
|
|
409
|
+
const finalKey = this.parentKey
|
|
410
|
+
? `${this.parentKey}[${index}]`
|
|
411
|
+
: `[${index}]`;
|
|
412
|
+
if (value instanceof JsonArray || value instanceof JsonObject) {
|
|
413
|
+
yield* new JsonKeyValueParser(this.buffer, value, finalKey).parse();
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
yield [finalKey, value.read()];
|
|
417
|
+
}
|
|
418
|
+
index++;
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
else if (this.container instanceof JsonValue) {
|
|
422
|
+
const value = this.container.read();
|
|
423
|
+
if (!(value instanceof JsonArray) &&
|
|
424
|
+
!(value instanceof JsonObject)) {
|
|
425
|
+
throw new Error(`JsonValue does not contain a composite type. ${value.entityType} found.`);
|
|
426
|
+
}
|
|
427
|
+
yield* new JsonKeyValueParser(this.buffer, value, this.parentKey).parse();
|
|
428
|
+
}
|
|
429
|
+
}
|
|
430
|
+
async *parseAsync() {
|
|
431
|
+
while (!this.buffer.atEof()) {
|
|
432
|
+
await this.buffer.readStream();
|
|
433
|
+
const parserGen = this.parse();
|
|
434
|
+
let currentPair = undefined;
|
|
435
|
+
while (true) {
|
|
436
|
+
currentPair = this.tryParse(() => parserGen.next());
|
|
437
|
+
if (currentPair === undefined) {
|
|
438
|
+
break; // Need more data
|
|
439
|
+
}
|
|
440
|
+
if (currentPair.done) {
|
|
441
|
+
return; // No more pairs
|
|
442
|
+
}
|
|
443
|
+
yield currentPair.value;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
[Symbol.iterator]() {
|
|
448
|
+
return this.parse();
|
|
449
|
+
}
|
|
450
|
+
[Symbol.asyncIterator]() {
|
|
451
|
+
return this.parseAsync();
|
|
452
|
+
}
|
|
453
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
type JsonStreamStringifyOptions = {
|
|
2
|
+
stringChunkSize?: number;
|
|
3
|
+
};
|
|
4
|
+
export declare function jsonStreamStringify(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<string>;
|
|
5
|
+
export declare function jsonStreamStringifyBytes(value: unknown, replacer?: any, indent?: number, options?: JsonStreamStringifyOptions): Generator<Uint8Array>;
|
|
6
|
+
export {};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { stringToBytes } from './utils';
|
|
2
|
+
function* formatString(str, chunkSize = 1024) {
|
|
3
|
+
const formatted = `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t')}"`;
|
|
4
|
+
let i = 0;
|
|
5
|
+
const len = formatted.length;
|
|
6
|
+
if (len === 0) {
|
|
7
|
+
yield '""';
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
while (i < len) {
|
|
11
|
+
const chunk = formatted.slice(i, i + chunkSize);
|
|
12
|
+
yield chunk;
|
|
13
|
+
i += chunkSize;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth = 0, options) {
|
|
17
|
+
if (replacer) {
|
|
18
|
+
value = replacer('', value);
|
|
19
|
+
}
|
|
20
|
+
if (value === null) {
|
|
21
|
+
yield 'null';
|
|
22
|
+
}
|
|
23
|
+
else if (typeof value === 'boolean') {
|
|
24
|
+
yield value ? 'true' : 'false';
|
|
25
|
+
}
|
|
26
|
+
else if (typeof value === 'number') {
|
|
27
|
+
yield Number.isFinite(value) ? value.toString() : 'null';
|
|
28
|
+
}
|
|
29
|
+
else if (typeof value === 'string') {
|
|
30
|
+
yield* formatString(value, options?.stringChunkSize);
|
|
31
|
+
}
|
|
32
|
+
else if (Array.isArray(value)) {
|
|
33
|
+
yield '[';
|
|
34
|
+
const len = value.length;
|
|
35
|
+
const valueIndent = indent * (currentDepth + 1);
|
|
36
|
+
for (let i = 0; i < len; i++) {
|
|
37
|
+
const next = value[i];
|
|
38
|
+
if (i > 0) {
|
|
39
|
+
yield ',';
|
|
40
|
+
}
|
|
41
|
+
if (indent > 0) {
|
|
42
|
+
yield '\n' + ' '.repeat(valueIndent);
|
|
43
|
+
}
|
|
44
|
+
if (next === undefined) {
|
|
45
|
+
yield 'null';
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, options);
|
|
49
|
+
}
|
|
50
|
+
if (indent > 0 && len > 0) {
|
|
51
|
+
yield '\n' + ' '.repeat(indent * currentDepth);
|
|
52
|
+
}
|
|
53
|
+
yield ']';
|
|
54
|
+
}
|
|
55
|
+
else if (typeof value === 'object') {
|
|
56
|
+
yield '{';
|
|
57
|
+
const keys = Object.keys(value);
|
|
58
|
+
const len = keys.length;
|
|
59
|
+
const valueIndent = indent * (currentDepth + 1);
|
|
60
|
+
let count = 0;
|
|
61
|
+
for (let i = 0; i < len; i++) {
|
|
62
|
+
const key = keys[i];
|
|
63
|
+
const next = value[key];
|
|
64
|
+
if (next === undefined) {
|
|
65
|
+
continue;
|
|
66
|
+
}
|
|
67
|
+
if (count > 0) {
|
|
68
|
+
yield ',';
|
|
69
|
+
}
|
|
70
|
+
if (indent > 0) {
|
|
71
|
+
yield '\n' + ' '.repeat(valueIndent);
|
|
72
|
+
}
|
|
73
|
+
yield* formatString(key, options?.stringChunkSize);
|
|
74
|
+
yield `:${indent > 0 ? ' ' : ''}`;
|
|
75
|
+
yield* jsonStreamStringifyWithDepth(next, replacer, indent > 0 ? indent : 0, currentDepth + 1, options);
|
|
76
|
+
count++;
|
|
77
|
+
}
|
|
78
|
+
if (indent > 0 && len > 0) {
|
|
79
|
+
yield '\n' + ' '.repeat(indent * currentDepth);
|
|
80
|
+
}
|
|
81
|
+
yield '}';
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function jsonStreamStringify(value, replacer, indent = 0, options) {
|
|
85
|
+
return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
|
|
86
|
+
}
|
|
87
|
+
export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options) {
|
|
88
|
+
const stringGenerator = jsonStreamStringify(value, replacer, indent, options);
|
|
89
|
+
for (const chunk of stringGenerator) {
|
|
90
|
+
yield stringToBytes(chunk);
|
|
91
|
+
}
|
|
92
|
+
}
|
package/dist/types.d.ts
ADDED
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/dist/utils.d.ts
ADDED
package/dist/utils.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export function stringToBytes(str) {
|
|
2
|
+
return new TextEncoder().encode(str);
|
|
3
|
+
}
|
|
4
|
+
export function bytesToString(bytes) {
|
|
5
|
+
return new TextDecoder().decode(bytes);
|
|
6
|
+
}
|
|
7
|
+
export function bytesToNumber(bytes) {
|
|
8
|
+
const str = bytesToString(bytes);
|
|
9
|
+
return Number(str);
|
|
10
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "json-stream-lite",
|
|
3
|
+
"version": "1.0.0",
|
|
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
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"default": "./dist/index.js"
|
|
11
|
+
},
|
|
12
|
+
"./*": {
|
|
13
|
+
"types": "./dist/*.d.ts",
|
|
14
|
+
"default": "./dist/*"
|
|
15
|
+
}
|
|
16
|
+
},
|
|
17
|
+
"keywords": [],
|
|
18
|
+
"author": "",
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"devDependencies": {
|
|
21
|
+
"typescript": "^5.9.3",
|
|
22
|
+
"vitest": "^4.0.14",
|
|
23
|
+
"@vitest/browser": "^4.0.14",
|
|
24
|
+
"@vitest/browser-playwright": "^4.0.14",
|
|
25
|
+
"@vitest/coverage-v8": "^4.0.14",
|
|
26
|
+
"playwright": "^1.57.0"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist/"
|
|
30
|
+
],
|
|
31
|
+
"scripts": {
|
|
32
|
+
"test:unit": "vitest run test/unit",
|
|
33
|
+
"test": "vitest run",
|
|
34
|
+
"compile": "pnpm compile:validate && tsc -p tsconfig.prod.json",
|
|
35
|
+
"compile:validate": "tsc -p tsconfig.json --noEmit",
|
|
36
|
+
"watch": "tsc -watch -p tsconfig.prod.json",
|
|
37
|
+
"watch:all": "tsc -watch -p tsconfig.json"
|
|
38
|
+
}
|
|
39
|
+
}
|