json-stream-lite 1.1.2 → 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.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
- 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
+ }
42
49
  }
43
50
  }
44
51
  parts.push('"');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.1.2",
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",