json-stream-lite 1.1.1 → 1.1.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 CHANGED
@@ -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/parser.d.ts CHANGED
@@ -102,11 +102,47 @@ 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
+ */
105
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
+ */
106
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
+ */
107
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
+ */
108
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
+ */
109
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
+ */
110
146
  [Symbol.asyncIterator](): AsyncGenerator<string, any, any>;
111
147
  }
112
148
  /**
package/dist/parser.js CHANGED
@@ -242,6 +242,13 @@ export class JsonString extends JsonEntity {
242
242
  const chunk = bytes.next().value;
243
243
  return bytesToString(chunk);
244
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
+ */
245
252
  *streamBytes(chunkSize = 1024) {
246
253
  if (this.buffer.peek() === BYTE_MAP.quotes) {
247
254
  this.buffer.next(); // consume quotes
@@ -253,27 +260,61 @@ export class JsonString extends JsonEntity {
253
260
  switch (this.buffer.peek()) {
254
261
  case BYTE_MAP.quotes:
255
262
  bytes.push(BYTE_MAP.quotes); // quotes
263
+ this.buffer.next(); // consume escaped character
256
264
  break;
257
265
  case BYTE_MAP.backslash:
258
266
  bytes.push(BYTE_MAP.backslash); // backslash
267
+ this.buffer.next(); // consume escaped character
259
268
  break;
260
269
  case BYTE_MAP.b:
261
270
  bytes.push(BYTE_MAP.backspace); // backspace
271
+ this.buffer.next(); // consume escaped character
262
272
  break;
263
273
  case BYTE_MAP.f:
264
274
  bytes.push(BYTE_MAP.formFeed); // form feed
275
+ this.buffer.next(); // consume escaped character
265
276
  break;
266
277
  case BYTE_MAP.n:
267
278
  bytes.push(BYTE_MAP.lineFeed); // line feed
279
+ this.buffer.next(); // consume escaped character
268
280
  break;
269
281
  case BYTE_MAP.r:
270
282
  bytes.push(BYTE_MAP.carriageReturn); // carriage return
283
+ this.buffer.next(); // consume escaped character
271
284
  break;
272
285
  case BYTE_MAP.t:
273
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();
274
316
  break;
275
317
  }
276
- this.buffer.next(); // consume escaped character
277
318
  }
278
319
  else {
279
320
  bytes.push(byte);
@@ -292,6 +333,14 @@ export class JsonString extends JsonEntity {
292
333
  yield result;
293
334
  }
294
335
  }
336
+ /**
337
+ * Async generator that yields the string content as raw byte chunks from a stream.
338
+ * Handles escape sequences by converting them to their actual byte values.
339
+ * Waits for more data from the stream when needed.
340
+ *
341
+ * @param chunkSize - The maximum size of each yielded byte array chunk (defaults to 1024)
342
+ * @yields Uint8Array chunks containing the unescaped string bytes
343
+ */
295
344
  async *streamBytesAsync(chunkSize = 1024) {
296
345
  while (!this.buffer.atEof()) {
297
346
  const stream = this.streamBytes(chunkSize);
@@ -308,19 +357,40 @@ export class JsonString extends JsonEntity {
308
357
  }
309
358
  }
310
359
  }
360
+ /**
361
+ * Generator that yields the string content as chunks of strings.
362
+ *
363
+ * @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
364
+ * @yields Chunks of the string content
365
+ */
311
366
  *stream(chunkSize = 1024) {
312
367
  for (const chunk of this.streamBytes(chunkSize)) {
313
368
  yield bytesToString(chunk);
314
369
  }
315
370
  }
371
+ /**
372
+ * Async generator that yields the string content as chunks of strings.
373
+ * @param chunkSize - The maximum size of each yielded string chunk (defaults to 1024)
374
+ * @yields Chunks of the string content
375
+ */
316
376
  async *streamAsync(chunkSize = 1024) {
317
377
  for await (const chunk of this.streamBytesAsync(chunkSize)) {
318
378
  yield bytesToString(chunk);
319
379
  }
320
380
  }
381
+ /**
382
+ * Returns an iterator for string chunks.
383
+ * Enables use of for...of loops on JsonString.
384
+ * @yields Chunks of the string content
385
+ */
321
386
  [Symbol.iterator]() {
322
387
  return this.stream();
323
388
  }
389
+ /**
390
+ * Returns an async iterator for string chunks.
391
+ * Enables use of for await...of loops on JsonString.
392
+ * @yields Chunks of the string content
393
+ */
324
394
  [Symbol.asyncIterator]() {
325
395
  return this.streamAsync();
326
396
  }
@@ -448,7 +518,8 @@ export class JsonValue extends JsonEntity {
448
518
  }
449
519
  else {
450
520
  throw new Error('Unexpected token while parsing JSON value: ' +
451
- String.fromCharCode(next ?? 0));
521
+ String.fromCharCode(next ?? 0) +
522
+ ` (${next})`);
452
523
  }
453
524
  }
454
525
  /**
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('\\\\');
@@ -31,8 +32,20 @@ function* formatString(str, chunkSize = 1024) {
31
32
  case '\t':
32
33
  parts.push('\\t');
33
34
  break;
35
+ case '\b':
36
+ parts.push('\\b');
37
+ break;
38
+ case '\f':
39
+ parts.push('\\f');
40
+ break;
34
41
  default:
35
- parts.push(char);
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
+ }
36
49
  }
37
50
  }
38
51
  parts.push('"');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.1.1",
3
+ "version": "1.1.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",