json-stream-lite 1.1.0 → 1.1.2
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/EXAMPLES.md +42 -0
- package/README.md +4 -0
- package/dist/parser.d.ts +42 -0
- package/dist/parser.js +126 -8
- package/dist/stringify.js +6 -0
- package/package.json +1 -1
package/EXAMPLES.md
CHANGED
|
@@ -1125,3 +1125,45 @@ generateReport()
|
|
|
1125
1125
|
|
|
1126
1126
|
console.log('\n=== All scenarios completed ===')
|
|
1127
1127
|
```
|
|
1128
|
+
|
|
1129
|
+
## Streaming JSON strings (advanced usage)
|
|
1130
|
+
|
|
1131
|
+
```typescript
|
|
1132
|
+
/**
|
|
1133
|
+
* Example demonstrating streaming JSON string parsing using json-stream-lite.
|
|
1134
|
+
*/
|
|
1135
|
+
|
|
1136
|
+
import { JsonString } from 'json-stream-lite'
|
|
1137
|
+
|
|
1138
|
+
const jsonString = '"Streamed JSON String Example"'
|
|
1139
|
+
|
|
1140
|
+
const jsonObject = new JsonString(
|
|
1141
|
+
(async function* () {
|
|
1142
|
+
for (const chunk of new TextEncoder().encode(jsonString)) {
|
|
1143
|
+
yield chunk
|
|
1144
|
+
}
|
|
1145
|
+
})(),
|
|
1146
|
+
)
|
|
1147
|
+
|
|
1148
|
+
for await (const streamChunk of jsonObject.streamAsync(2)) {
|
|
1149
|
+
console.log('Received string chunk:', streamChunk)
|
|
1150
|
+
}
|
|
1151
|
+
|
|
1152
|
+
/**
|
|
1153
|
+
* Expected Output:
|
|
1154
|
+
* Received string chunk: St
|
|
1155
|
+
* Received string chunk: re
|
|
1156
|
+
* Received string chunk: am
|
|
1157
|
+
* Received string chunk: ed
|
|
1158
|
+
* Received string chunk: J
|
|
1159
|
+
* Received string chunk: JS
|
|
1160
|
+
* Received string chunk: ON
|
|
1161
|
+
* Received string chunk: St
|
|
1162
|
+
* Received string chunk: ri
|
|
1163
|
+
* Received string chunk: ng
|
|
1164
|
+
* Received string chunk: E
|
|
1165
|
+
* Received string chunk: xa
|
|
1166
|
+
* Received string chunk: mp
|
|
1167
|
+
* Received string chunk: le
|
|
1168
|
+
*/
|
|
1169
|
+
```
|
package/README.md
CHANGED
|
@@ -268,6 +268,10 @@ const result = parser.read()
|
|
|
268
268
|
console.log(result) // { name: 'Alice' }
|
|
269
269
|
```
|
|
270
270
|
|
|
271
|
+
### Stream Strings
|
|
272
|
+
|
|
273
|
+
See [string streaming](./EXAMPLES.md#streaming-json-strings) for advanced string handling.
|
|
274
|
+
|
|
271
275
|
## Use Cases
|
|
272
276
|
|
|
273
277
|
### 1. Processing API Responses
|
package/dist/parser.d.ts
CHANGED
|
@@ -102,6 +102,48 @@ export declare class JsonString<T extends string = string> extends JsonEntity<T>
|
|
|
102
102
|
* @returns The parsed string value
|
|
103
103
|
*/
|
|
104
104
|
protected parse(): T;
|
|
105
|
+
/**
|
|
106
|
+
* Generator that yields the string content as raw byte chunks.
|
|
107
|
+
* Handles escape sequences by converting them to their actual byte values.
|
|
108
|
+
*
|
|
109
|
+
* @param chunkSize - The maximum size of each yielded byte array chunk (defaults to 1024)
|
|
110
|
+
* @yields Uint8Array chunks containing the unescaped string bytes
|
|
111
|
+
*/
|
|
112
|
+
streamBytes(chunkSize?: number): Generator<Uint8Array>;
|
|
113
|
+
/**
|
|
114
|
+
* Async generator that yields the string content as raw byte chunks from a stream.
|
|
115
|
+
* Handles escape sequences by converting them to their actual byte values.
|
|
116
|
+
* Waits for more data from the stream when needed.
|
|
117
|
+
*
|
|
118
|
+
* @param chunkSize - The maximum size of each yielded byte array chunk (defaults to 1024)
|
|
119
|
+
* @yields Uint8Array chunks containing the unescaped string bytes
|
|
120
|
+
*/
|
|
121
|
+
streamBytesAsync(chunkSize?: number): AsyncGenerator<Uint8Array>;
|
|
122
|
+
/**
|
|
123
|
+
* Generator that yields the string content as chunks of strings.
|
|
124
|
+
*
|
|
125
|
+
* @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
|
|
126
|
+
* @yields Chunks of the string content
|
|
127
|
+
*/
|
|
128
|
+
stream(chunkSize?: number): Generator<string>;
|
|
129
|
+
/**
|
|
130
|
+
* Async generator that yields the string content as chunks of strings.
|
|
131
|
+
* @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
|
|
132
|
+
* @yields Chunks of the string content
|
|
133
|
+
*/
|
|
134
|
+
streamAsync(chunkSize?: number): AsyncGenerator<string>;
|
|
135
|
+
/**
|
|
136
|
+
* Returns an iterator for string chunks.
|
|
137
|
+
* Enables use of for...of loops on JsonString.
|
|
138
|
+
* @yields Chunks of the string content
|
|
139
|
+
*/
|
|
140
|
+
[Symbol.iterator](): Generator<string, any, any>;
|
|
141
|
+
/**
|
|
142
|
+
* Returns an async iterator for string chunks.
|
|
143
|
+
* Enables use of for await...of loops on JsonString.
|
|
144
|
+
* @yields Chunks of the string content
|
|
145
|
+
*/
|
|
146
|
+
[Symbol.asyncIterator](): AsyncGenerator<string, any, any>;
|
|
105
147
|
}
|
|
106
148
|
/**
|
|
107
149
|
* Represents a JSON number value.
|
package/dist/parser.js
CHANGED
|
@@ -14,6 +14,7 @@ const BYTE_MAP = {
|
|
|
14
14
|
r: 114,
|
|
15
15
|
l: 108,
|
|
16
16
|
s: 115,
|
|
17
|
+
b: 98,
|
|
17
18
|
zero: 48,
|
|
18
19
|
nine: 57,
|
|
19
20
|
minus: 45,
|
|
@@ -31,6 +32,9 @@ const BYTE_MAP = {
|
|
|
31
32
|
rightSquare: 93,
|
|
32
33
|
comma: 44,
|
|
33
34
|
colon: 58,
|
|
35
|
+
backslash: 92,
|
|
36
|
+
formFeed: 12,
|
|
37
|
+
backspace: 8,
|
|
34
38
|
};
|
|
35
39
|
/**
|
|
36
40
|
* Checks if a byte represents a whitespace character.
|
|
@@ -216,7 +220,7 @@ export class JsonEntity {
|
|
|
216
220
|
const res = cb(this);
|
|
217
221
|
this.buffer.locked = false;
|
|
218
222
|
return res;
|
|
219
|
-
}, (
|
|
223
|
+
}, () => {
|
|
220
224
|
this.buffer.locked = false;
|
|
221
225
|
});
|
|
222
226
|
}
|
|
@@ -234,14 +238,127 @@ export class JsonString extends JsonEntity {
|
|
|
234
238
|
* @returns The parsed string value
|
|
235
239
|
*/
|
|
236
240
|
parse() {
|
|
241
|
+
const bytes = this.streamBytes(Number.MAX_SAFE_INTEGER);
|
|
242
|
+
const chunk = bytes.next().value;
|
|
243
|
+
return bytesToString(chunk);
|
|
244
|
+
}
|
|
245
|
+
/**
|
|
246
|
+
* Generator that yields the string content as raw byte chunks.
|
|
247
|
+
* Handles escape sequences by converting them to their actual byte values.
|
|
248
|
+
*
|
|
249
|
+
* @param chunkSize - The maximum size of each yielded byte array chunk (defaults to 1024)
|
|
250
|
+
* @yields Uint8Array chunks containing the unescaped string bytes
|
|
251
|
+
*/
|
|
252
|
+
*streamBytes(chunkSize = 1024) {
|
|
253
|
+
if (this.buffer.peek() === BYTE_MAP.quotes) {
|
|
254
|
+
this.buffer.next(); // consume quotes
|
|
255
|
+
}
|
|
237
256
|
const bytes = [];
|
|
238
|
-
this.buffer.
|
|
239
|
-
|
|
240
|
-
|
|
257
|
+
while (!this.buffer.atEof() && this.buffer.peek() !== BYTE_MAP.quotes) {
|
|
258
|
+
const byte = this.buffer.next();
|
|
259
|
+
if (byte === BYTE_MAP.backslash) {
|
|
260
|
+
switch (this.buffer.peek()) {
|
|
261
|
+
case BYTE_MAP.quotes:
|
|
262
|
+
bytes.push(BYTE_MAP.quotes); // quotes
|
|
263
|
+
break;
|
|
264
|
+
case BYTE_MAP.backslash:
|
|
265
|
+
bytes.push(BYTE_MAP.backslash); // backslash
|
|
266
|
+
break;
|
|
267
|
+
case BYTE_MAP.b:
|
|
268
|
+
bytes.push(BYTE_MAP.backspace); // backspace
|
|
269
|
+
break;
|
|
270
|
+
case BYTE_MAP.f:
|
|
271
|
+
bytes.push(BYTE_MAP.formFeed); // form feed
|
|
272
|
+
break;
|
|
273
|
+
case BYTE_MAP.n:
|
|
274
|
+
bytes.push(BYTE_MAP.lineFeed); // line feed
|
|
275
|
+
break;
|
|
276
|
+
case BYTE_MAP.r:
|
|
277
|
+
bytes.push(BYTE_MAP.carriageReturn); // carriage return
|
|
278
|
+
break;
|
|
279
|
+
case BYTE_MAP.t:
|
|
280
|
+
bytes.push(BYTE_MAP.tab); // tab
|
|
281
|
+
break;
|
|
282
|
+
}
|
|
283
|
+
this.buffer.next(); // consume escaped character
|
|
284
|
+
}
|
|
285
|
+
else {
|
|
286
|
+
bytes.push(byte);
|
|
287
|
+
}
|
|
288
|
+
if (bytes.length >= chunkSize) {
|
|
289
|
+
const result = new Uint8Array(bytes);
|
|
290
|
+
bytes.length = 0;
|
|
291
|
+
yield result;
|
|
292
|
+
}
|
|
241
293
|
}
|
|
242
|
-
this.buffer.
|
|
243
|
-
|
|
244
|
-
|
|
294
|
+
if (this.buffer.peek() === BYTE_MAP.quotes) {
|
|
295
|
+
this.buffer.next(); // consume quotes
|
|
296
|
+
}
|
|
297
|
+
if (bytes.length > 0) {
|
|
298
|
+
const result = new Uint8Array(bytes);
|
|
299
|
+
yield result;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Async generator that yields the string content as raw byte chunks from a stream.
|
|
304
|
+
* Handles escape sequences by converting them to their actual byte values.
|
|
305
|
+
* Waits for more data from the stream when needed.
|
|
306
|
+
*
|
|
307
|
+
* @param chunkSize - The maximum size of each yielded byte array chunk (defaults to 1024)
|
|
308
|
+
* @yields Uint8Array chunks containing the unescaped string bytes
|
|
309
|
+
*/
|
|
310
|
+
async *streamBytesAsync(chunkSize = 1024) {
|
|
311
|
+
while (!this.buffer.atEof()) {
|
|
312
|
+
const stream = this.streamBytes(chunkSize);
|
|
313
|
+
while (true) {
|
|
314
|
+
const next = this.tryParse(() => stream.next());
|
|
315
|
+
if (next === undefined) {
|
|
316
|
+
await this.buffer.readStreamAsync();
|
|
317
|
+
break;
|
|
318
|
+
}
|
|
319
|
+
if (next.done) {
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
yield next.value;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
/**
|
|
327
|
+
* Generator that yields the string content as chunks of strings.
|
|
328
|
+
*
|
|
329
|
+
* @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
|
|
330
|
+
* @yields Chunks of the string content
|
|
331
|
+
*/
|
|
332
|
+
*stream(chunkSize = 1024) {
|
|
333
|
+
for (const chunk of this.streamBytes(chunkSize)) {
|
|
334
|
+
yield bytesToString(chunk);
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Async generator that yields the string content as chunks of strings.
|
|
339
|
+
* @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
|
|
340
|
+
* @yields Chunks of the string content
|
|
341
|
+
*/
|
|
342
|
+
async *streamAsync(chunkSize = 1024) {
|
|
343
|
+
for await (const chunk of this.streamBytesAsync(chunkSize)) {
|
|
344
|
+
yield bytesToString(chunk);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* Returns an iterator for string chunks.
|
|
349
|
+
* Enables use of for...of loops on JsonString.
|
|
350
|
+
* @yields Chunks of the string content
|
|
351
|
+
*/
|
|
352
|
+
[Symbol.iterator]() {
|
|
353
|
+
return this.stream();
|
|
354
|
+
}
|
|
355
|
+
/**
|
|
356
|
+
* Returns an async iterator for string chunks.
|
|
357
|
+
* Enables use of for await...of loops on JsonString.
|
|
358
|
+
* @yields Chunks of the string content
|
|
359
|
+
*/
|
|
360
|
+
[Symbol.asyncIterator]() {
|
|
361
|
+
return this.streamAsync();
|
|
245
362
|
}
|
|
246
363
|
}
|
|
247
364
|
/**
|
|
@@ -367,7 +484,8 @@ export class JsonValue extends JsonEntity {
|
|
|
367
484
|
}
|
|
368
485
|
else {
|
|
369
486
|
throw new Error('Unexpected token while parsing JSON value: ' +
|
|
370
|
-
String.fromCharCode(next ?? 0)
|
|
487
|
+
String.fromCharCode(next ?? 0) +
|
|
488
|
+
` (${next})`);
|
|
371
489
|
}
|
|
372
490
|
}
|
|
373
491
|
/**
|
package/dist/stringify.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.2",
|
|
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",
|