json-stream-lite 1.1.0 → 1.1.1
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 +6 -0
- package/dist/parser.js +88 -7
- 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,12 @@ 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
|
+
streamBytes(chunkSize?: number): Generator<Uint8Array>;
|
|
106
|
+
streamBytesAsync(chunkSize?: number): AsyncGenerator<Uint8Array>;
|
|
107
|
+
stream(chunkSize?: number): Generator<string>;
|
|
108
|
+
streamAsync(chunkSize?: number): AsyncGenerator<string>;
|
|
109
|
+
[Symbol.iterator](): Generator<string, any, any>;
|
|
110
|
+
[Symbol.asyncIterator](): AsyncGenerator<string, any, any>;
|
|
105
111
|
}
|
|
106
112
|
/**
|
|
107
113
|
* 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,91 @@ 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
|
+
*streamBytes(chunkSize = 1024) {
|
|
246
|
+
if (this.buffer.peek() === BYTE_MAP.quotes) {
|
|
247
|
+
this.buffer.next(); // consume quotes
|
|
248
|
+
}
|
|
237
249
|
const bytes = [];
|
|
238
|
-
this.buffer.
|
|
239
|
-
|
|
240
|
-
|
|
250
|
+
while (!this.buffer.atEof() && this.buffer.peek() !== BYTE_MAP.quotes) {
|
|
251
|
+
const byte = this.buffer.next();
|
|
252
|
+
if (byte === BYTE_MAP.backslash) {
|
|
253
|
+
switch (this.buffer.peek()) {
|
|
254
|
+
case BYTE_MAP.quotes:
|
|
255
|
+
bytes.push(BYTE_MAP.quotes); // quotes
|
|
256
|
+
break;
|
|
257
|
+
case BYTE_MAP.backslash:
|
|
258
|
+
bytes.push(BYTE_MAP.backslash); // backslash
|
|
259
|
+
break;
|
|
260
|
+
case BYTE_MAP.b:
|
|
261
|
+
bytes.push(BYTE_MAP.backspace); // backspace
|
|
262
|
+
break;
|
|
263
|
+
case BYTE_MAP.f:
|
|
264
|
+
bytes.push(BYTE_MAP.formFeed); // form feed
|
|
265
|
+
break;
|
|
266
|
+
case BYTE_MAP.n:
|
|
267
|
+
bytes.push(BYTE_MAP.lineFeed); // line feed
|
|
268
|
+
break;
|
|
269
|
+
case BYTE_MAP.r:
|
|
270
|
+
bytes.push(BYTE_MAP.carriageReturn); // carriage return
|
|
271
|
+
break;
|
|
272
|
+
case BYTE_MAP.t:
|
|
273
|
+
bytes.push(BYTE_MAP.tab); // tab
|
|
274
|
+
break;
|
|
275
|
+
}
|
|
276
|
+
this.buffer.next(); // consume escaped character
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
bytes.push(byte);
|
|
280
|
+
}
|
|
281
|
+
if (bytes.length >= chunkSize) {
|
|
282
|
+
const result = new Uint8Array(bytes);
|
|
283
|
+
bytes.length = 0;
|
|
284
|
+
yield result;
|
|
285
|
+
}
|
|
241
286
|
}
|
|
242
|
-
this.buffer.
|
|
243
|
-
|
|
244
|
-
|
|
287
|
+
if (this.buffer.peek() === BYTE_MAP.quotes) {
|
|
288
|
+
this.buffer.next(); // consume quotes
|
|
289
|
+
}
|
|
290
|
+
if (bytes.length > 0) {
|
|
291
|
+
const result = new Uint8Array(bytes);
|
|
292
|
+
yield result;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
async *streamBytesAsync(chunkSize = 1024) {
|
|
296
|
+
while (!this.buffer.atEof()) {
|
|
297
|
+
const stream = this.streamBytes(chunkSize);
|
|
298
|
+
while (true) {
|
|
299
|
+
const next = this.tryParse(() => stream.next());
|
|
300
|
+
if (next === undefined) {
|
|
301
|
+
await this.buffer.readStreamAsync();
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
if (next.done) {
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
yield next.value;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
*stream(chunkSize = 1024) {
|
|
312
|
+
for (const chunk of this.streamBytes(chunkSize)) {
|
|
313
|
+
yield bytesToString(chunk);
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
async *streamAsync(chunkSize = 1024) {
|
|
317
|
+
for await (const chunk of this.streamBytesAsync(chunkSize)) {
|
|
318
|
+
yield bytesToString(chunk);
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
[Symbol.iterator]() {
|
|
322
|
+
return this.stream();
|
|
323
|
+
}
|
|
324
|
+
[Symbol.asyncIterator]() {
|
|
325
|
+
return this.streamAsync();
|
|
245
326
|
}
|
|
246
327
|
}
|
|
247
328
|
/**
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.1",
|
|
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",
|