json-stream-lite 1.1.2 → 1.1.4
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 +3 -3
- package/dist/byte-buffer.d.ts +2 -2
- package/dist/byte-buffer.js +4 -6
- package/dist/parser.d.ts +3 -3
- package/dist/parser.js +35 -1
- package/dist/stringify.js +8 -1
- package/dist/types.d.ts +3 -3
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -32,7 +32,7 @@ yarn add json-stream-lite
|
|
|
32
32
|
|
|
33
33
|
### Parsing JSON
|
|
34
34
|
|
|
35
|
-
#### Parse a complete JSON object
|
|
35
|
+
#### Parse a complete JSON object
|
|
36
36
|
|
|
37
37
|
```typescript
|
|
38
38
|
import { JsonObject } from 'json-stream-lite'
|
|
@@ -48,7 +48,7 @@ const result = parser.read()
|
|
|
48
48
|
console.log(result) // { name: 'Alice', age: 30, active: true }
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
#### Stream through object members
|
|
51
|
+
#### Stream through object members incrementally
|
|
52
52
|
|
|
53
53
|
```typescript
|
|
54
54
|
import { JsonObject } from 'json-stream-lite'
|
|
@@ -270,7 +270,7 @@ console.log(result) // { name: 'Alice' }
|
|
|
270
270
|
|
|
271
271
|
### Stream Strings
|
|
272
272
|
|
|
273
|
-
See [string streaming](./EXAMPLES.md#streaming-json-strings) for advanced string handling.
|
|
273
|
+
See [string streaming](./EXAMPLES.md#streaming-json-strings-advanced-usage) for advanced string handling.
|
|
274
274
|
|
|
275
275
|
## Use Cases
|
|
276
276
|
|
package/dist/byte-buffer.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { ByteStream,
|
|
1
|
+
import type { ByteStream, StreamInput } from './types.js';
|
|
2
2
|
/**
|
|
3
3
|
* A buffer for managing byte-level input with support for async streams.
|
|
4
4
|
* Provides lookahead, consumption tracking, and buffer compaction capabilities.
|
|
@@ -49,7 +49,7 @@ export declare class ByteBuffer {
|
|
|
49
49
|
*
|
|
50
50
|
* @param input - Input items to add to the buffer
|
|
51
51
|
*/
|
|
52
|
-
feed(...input:
|
|
52
|
+
feed(...input: StreamInput[]): void;
|
|
53
53
|
push(byte: number): void;
|
|
54
54
|
/**
|
|
55
55
|
* Checks if end of file has been reached and buffer is exhausted.
|
package/dist/byte-buffer.js
CHANGED
|
@@ -185,13 +185,11 @@ export class ByteBuffer {
|
|
|
185
185
|
*/
|
|
186
186
|
next() {
|
|
187
187
|
if (this.bufferIndex >= this.buffer.length) {
|
|
188
|
+
if (this.readStream()) {
|
|
189
|
+
return this.next();
|
|
190
|
+
}
|
|
188
191
|
if (!this.eof) {
|
|
189
|
-
|
|
190
|
-
throw new NoMoreTokensError('No more items available');
|
|
191
|
-
}
|
|
192
|
-
else {
|
|
193
|
-
return this.next();
|
|
194
|
-
}
|
|
192
|
+
throw new NoMoreTokensError('No more items available');
|
|
195
193
|
}
|
|
196
194
|
throw new EofReachedError('End of file reached');
|
|
197
195
|
}
|
package/dist/parser.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ByteBuffer } from './byte-buffer.js';
|
|
2
|
-
import { ByteStream, JsonKeyValuePair,
|
|
2
|
+
import { ByteStream, JsonKeyValuePair, StreamInput } from './types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Abstract base class for all JSON entities.
|
|
5
5
|
* Provides common functionality for parsing, reading, and consuming JSON values.
|
|
@@ -46,7 +46,7 @@ export declare abstract class JsonEntity<T> {
|
|
|
46
46
|
*
|
|
47
47
|
* @param input - One or more strings, numbers, arrays of numbers, or Uint8Arrays to add to the buffer
|
|
48
48
|
*/
|
|
49
|
-
feed(...input:
|
|
49
|
+
feed(...input: StreamInput[]): void;
|
|
50
50
|
/**
|
|
51
51
|
* Abstract method that subclasses must implement to parse their specific type.
|
|
52
52
|
*
|
|
@@ -188,7 +188,7 @@ export declare class JsonNull extends JsonEntity<null> {
|
|
|
188
188
|
*/
|
|
189
189
|
export type JsonPrimitiveType = JsonString | JsonNumber | JsonBoolean | JsonNull;
|
|
190
190
|
/**
|
|
191
|
-
*
|
|
191
|
+
* Converts a standard JS primitive type into a json-stream-lite parser type
|
|
192
192
|
*
|
|
193
193
|
* @typeParam T - The expected type of the value
|
|
194
194
|
*/
|
package/dist/parser.js
CHANGED
|
@@ -260,27 +260,61 @@ export class JsonString extends JsonEntity {
|
|
|
260
260
|
switch (this.buffer.peek()) {
|
|
261
261
|
case BYTE_MAP.quotes:
|
|
262
262
|
bytes.push(BYTE_MAP.quotes); // quotes
|
|
263
|
+
this.buffer.next(); // consume escaped character
|
|
263
264
|
break;
|
|
264
265
|
case BYTE_MAP.backslash:
|
|
265
266
|
bytes.push(BYTE_MAP.backslash); // backslash
|
|
267
|
+
this.buffer.next(); // consume escaped character
|
|
266
268
|
break;
|
|
267
269
|
case BYTE_MAP.b:
|
|
268
270
|
bytes.push(BYTE_MAP.backspace); // backspace
|
|
271
|
+
this.buffer.next(); // consume escaped character
|
|
269
272
|
break;
|
|
270
273
|
case BYTE_MAP.f:
|
|
271
274
|
bytes.push(BYTE_MAP.formFeed); // form feed
|
|
275
|
+
this.buffer.next(); // consume escaped character
|
|
272
276
|
break;
|
|
273
277
|
case BYTE_MAP.n:
|
|
274
278
|
bytes.push(BYTE_MAP.lineFeed); // line feed
|
|
279
|
+
this.buffer.next(); // consume escaped character
|
|
275
280
|
break;
|
|
276
281
|
case BYTE_MAP.r:
|
|
277
282
|
bytes.push(BYTE_MAP.carriageReturn); // carriage return
|
|
283
|
+
this.buffer.next(); // consume escaped character
|
|
278
284
|
break;
|
|
279
285
|
case BYTE_MAP.t:
|
|
280
286
|
bytes.push(BYTE_MAP.tab); // tab
|
|
287
|
+
this.buffer.next(); // consume escaped character
|
|
288
|
+
break;
|
|
289
|
+
case BYTE_MAP.u:
|
|
290
|
+
// Unicode escape sequence \uXXXX
|
|
291
|
+
this.buffer.next(); // consume 'u'
|
|
292
|
+
const hex1 = this.buffer.next();
|
|
293
|
+
const hex2 = this.buffer.next();
|
|
294
|
+
const hex3 = this.buffer.next();
|
|
295
|
+
const hex4 = this.buffer.next();
|
|
296
|
+
// Convert 4 hex digits to a character code
|
|
297
|
+
const hexString = String.fromCharCode(hex1, hex2, hex3, hex4);
|
|
298
|
+
const charCode = parseInt(hexString, 16);
|
|
299
|
+
if (!isNaN(charCode)) {
|
|
300
|
+
// For characters that fit in a single byte, push the byte
|
|
301
|
+
// For multi-byte UTF-8, we need to encode properly
|
|
302
|
+
if (charCode < 0x80) {
|
|
303
|
+
bytes.push(charCode);
|
|
304
|
+
}
|
|
305
|
+
else {
|
|
306
|
+
// UTF-8 encode the character
|
|
307
|
+
const char = String.fromCharCode(charCode);
|
|
308
|
+
const encoded = new TextEncoder().encode(char);
|
|
309
|
+
bytes.push(...encoded);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
break;
|
|
313
|
+
default:
|
|
314
|
+
// Unknown escape sequence - consume it
|
|
315
|
+
this.buffer.next();
|
|
281
316
|
break;
|
|
282
317
|
}
|
|
283
|
-
this.buffer.next(); // consume escaped character
|
|
284
318
|
}
|
|
285
319
|
else {
|
|
286
320
|
bytes.push(byte);
|
package/dist/stringify.js
CHANGED
|
@@ -15,6 +15,7 @@ function* formatString(str, chunkSize = 1024) {
|
|
|
15
15
|
const parts = ['"'];
|
|
16
16
|
for (let i = 0; i < str.length; i++) {
|
|
17
17
|
const char = str[i];
|
|
18
|
+
const charCode = str.charCodeAt(i);
|
|
18
19
|
switch (char) {
|
|
19
20
|
case '\\':
|
|
20
21
|
parts.push('\\\\');
|
|
@@ -38,7 +39,13 @@ function* formatString(str, chunkSize = 1024) {
|
|
|
38
39
|
parts.push('\\f');
|
|
39
40
|
break;
|
|
40
41
|
default:
|
|
41
|
-
|
|
42
|
+
// Escape control characters (0x00-0x1F) as \uXXXX
|
|
43
|
+
if (charCode < 0x20) {
|
|
44
|
+
parts.push('\\u' + charCode.toString(16).padStart(4, '0'));
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
parts.push(char);
|
|
48
|
+
}
|
|
42
49
|
}
|
|
43
50
|
}
|
|
44
51
|
parts.push('"');
|
package/dist/types.d.ts
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Union type representing valid
|
|
2
|
+
* Union type representing valid stream input formats.
|
|
3
3
|
*/
|
|
4
|
-
export type
|
|
4
|
+
export type StreamInput = string | number | number[] | Uint8Array;
|
|
5
5
|
/**
|
|
6
6
|
* An async iterable stream of JSON input that can be consumed incrementally.
|
|
7
7
|
* Supports strings, numbers, arrays of numbers, or Uint8Arrays as stream items.
|
|
8
8
|
*/
|
|
9
|
-
export type ByteStream = AsyncIterable<
|
|
9
|
+
export type ByteStream = AsyncIterable<StreamInput> | Iterable<StreamInput> | ReadableStream<StreamInput>;
|
|
10
10
|
/**
|
|
11
11
|
* Union type representing all JSON primitive value types.
|
|
12
12
|
*/
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.1.
|
|
3
|
+
"version": "1.1.4",
|
|
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",
|