json-stream-lite 1.0.5 → 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.
- package/dist/stringify.js +52 -8
- package/package.json +1 -1
package/dist/stringify.js
CHANGED
|
@@ -1,22 +1,65 @@
|
|
|
1
|
-
import { stringToBytes } from './utils.js';
|
|
2
1
|
function* formatString(str, chunkSize = 1024) {
|
|
3
|
-
|
|
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;
|
|
13
36
|
i += chunkSize;
|
|
14
37
|
}
|
|
15
38
|
}
|
|
39
|
+
function shouldIgnore(value) {
|
|
40
|
+
return typeof value === 'function' || value === undefined;
|
|
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
|
+
}
|
|
16
51
|
function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth = 0, options) {
|
|
17
52
|
if (replacer) {
|
|
18
53
|
value = replacer('', value);
|
|
19
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
|
+
}
|
|
20
63
|
if (value === null) {
|
|
21
64
|
yield 'null';
|
|
22
65
|
}
|
|
@@ -41,7 +84,7 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
41
84
|
if (indent > 0) {
|
|
42
85
|
yield '\n' + ' '.repeat(valueIndent);
|
|
43
86
|
}
|
|
44
|
-
if (next
|
|
87
|
+
if (shouldIgnore(next)) {
|
|
45
88
|
yield 'null';
|
|
46
89
|
continue;
|
|
47
90
|
}
|
|
@@ -61,7 +104,7 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
61
104
|
for (let i = 0; i < len; i++) {
|
|
62
105
|
const key = keys[i];
|
|
63
106
|
const next = value[key];
|
|
64
|
-
if (next
|
|
107
|
+
if (shouldIgnore(next)) {
|
|
65
108
|
continue;
|
|
66
109
|
}
|
|
67
110
|
if (count > 0) {
|
|
@@ -84,9 +127,10 @@ function* jsonStreamStringifyWithDepth(value, replacer, indent = 0, currentDepth
|
|
|
84
127
|
export function jsonStreamStringify(value, replacer, indent = 0, options) {
|
|
85
128
|
return jsonStreamStringifyWithDepth(value, replacer, indent, 0, options);
|
|
86
129
|
}
|
|
130
|
+
const TEXT_ENCODER = new TextEncoder();
|
|
87
131
|
export function* jsonStreamStringifyBytes(value, replacer, indent = 0, options) {
|
|
88
132
|
const stringGenerator = jsonStreamStringify(value, replacer, indent, options);
|
|
89
133
|
for (const chunk of stringGenerator) {
|
|
90
|
-
yield
|
|
134
|
+
yield TEXT_ENCODER.encode(chunk);
|
|
91
135
|
}
|
|
92
136
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "json-stream-lite",
|
|
3
|
-
"version": "1.0.
|
|
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",
|