json-stream-lite 1.0.6 → 1.0.7

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.
Files changed (2) hide show
  1. package/dist/stringify.js +47 -6
  2. package/package.json +1 -1
package/dist/stringify.js CHANGED
@@ -1,12 +1,35 @@
1
- import { stringToBytes } from './utils.js';
2
1
  function* formatString(str, chunkSize = 1024) {
3
- const formatted = `"${str.replace(/\\/g, '\\\\').replace(/"/g, '\\"').replace(/\n/g, '\\n').replace(/\r/g, '\\r').replace(/\t/g, '\\t')}"`;
4
- let i = 0;
5
- const len = formatted.length;
6
- if (len === 0) {
2
+ if (str.length === 0) {
7
3
  yield '""';
8
4
  return;
9
5
  }
6
+ const parts = ['"'];
7
+ for (let i = 0; i < str.length; i++) {
8
+ const char = str[i];
9
+ switch (char) {
10
+ case '\\':
11
+ parts.push('\\\\');
12
+ break;
13
+ case '"':
14
+ parts.push('\\"');
15
+ break;
16
+ case '\n':
17
+ parts.push('\\n');
18
+ break;
19
+ case '\r':
20
+ parts.push('\\r');
21
+ break;
22
+ case '\t':
23
+ parts.push('\\t');
24
+ break;
25
+ default:
26
+ parts.push(char);
27
+ }
28
+ }
29
+ parts.push('"');
30
+ const formatted = parts.join('');
31
+ let i = 0;
32
+ const len = formatted.length;
10
33
  while (i < len) {
11
34
  const chunk = formatted.slice(i, i + chunkSize);
12
35
  yield chunk;
@@ -16,10 +39,27 @@ function* formatString(str, chunkSize = 1024) {
16
39
  function shouldIgnore(value) {
17
40
  return typeof value === 'function' || value === undefined;
18
41
  }
42
+ function hasCustomToJsonFunction(value) {
43
+ return 'toJSON' in value && typeof value.toJSON === 'function';
44
+ }
45
+ function hasCustomToStringFunction(value) {
46
+ return ('toString' in value &&
47
+ typeof value.toString === 'function' &&
48
+ value.toString !== Object.prototype.toString &&
49
+ value.toString !== Array.prototype.toString);
50
+ }
19
51
  function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth = 0, options) {
20
52
  if (replacer) {
21
53
  value = replacer('', value);
22
54
  }
55
+ if (value && typeof value === 'object') {
56
+ if (hasCustomToJsonFunction(value)) {
57
+ value = value.toJSON();
58
+ }
59
+ else if (hasCustomToStringFunction(value)) {
60
+ value = value.toString();
61
+ }
62
+ }
23
63
  if (value === null) {
24
64
  yield 'null';
25
65
  }
@@ -87,9 +127,10 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
87
127
  export function jsonStreamStringify(value, replacer, indent = 0, options) {
88
128
  return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
89
129
  }
130
+ const TEXT_ENCODER = new TextEncoder();
90
131
  export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options) {
91
132
  const stringGenerator = jsonStreamStringify(value, replacer, indent, options);
92
133
  for (const chunk of stringGenerator) {
93
- yield stringToBytes(chunk);
134
+ yield TEXT_ENCODER.encode(chunk);
94
135
  }
95
136
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "json-stream-lite",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
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",