json-brook 0.0.1 → 0.0.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/CHANGELOG.md +12 -0
- package/README.md +35 -7
- package/dist/es/index.js +117 -257
- package/dist/es/internal.js +115 -255
- package/dist/lib/index.js +117 -257
- package/dist/lib/internal.js +115 -255
- package/dist/types/{internal-73736079.d.ts → internal-2ae957fd.d.ts} +2 -2
- package/dist/types/internal.d.ts +1 -1
- package/dist/types/type.d.ts +1 -1
- package/package.json +5 -2
package/CHANGELOG.md
CHANGED
package/README.md
CHANGED
|
@@ -1,12 +1,40 @@
|
|
|
1
1
|
# JsonBrook
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+

|
|
4
|
+
|
|
5
|
+
> 流式解析Json数据
|
|
4
6
|
> 实现上参考了[json-to-ast](https://github.com/vtrushin/json-to-ast)
|
|
5
7
|
|
|
6
|
-
##
|
|
8
|
+
## 快速入门
|
|
9
|
+
|
|
10
|
+
```typescript
|
|
11
|
+
import { createJsonBrook } from "json-brook";
|
|
12
|
+
|
|
13
|
+
const jsonBrook = createJsonBrook();
|
|
14
|
+
|
|
15
|
+
const sample = `{
|
|
16
|
+
"string": "welcome to json brook",
|
|
17
|
+
"number": 20241102,
|
|
18
|
+
"boolean": true,
|
|
19
|
+
"array": ["a", "b", "c"],
|
|
20
|
+
"null": null
|
|
21
|
+
}`;
|
|
22
|
+
|
|
23
|
+
for (const char of sample) {
|
|
24
|
+
jsonBrook.write(char);
|
|
25
|
+
console.log(jsonBrook.getCurrent());
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
jsonBrook.end();
|
|
29
|
+
console.log(jsonBrook.getCurrent());
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
## API
|
|
33
|
+
`createJsonBrook` 返回一个JsonBrook实例,JsonBrook实例具有以下方法:
|
|
34
|
+
* `write` 接受字符串并解析,如果解析失败会抛错
|
|
35
|
+
* `end` 结束输入并解析Json数据(针对比较极端的纯数字形式解析,需要知道当前输入已结束),如果解析失败会抛错
|
|
36
|
+
* `getCurrent` 获取当前解析结果
|
|
37
|
+
|
|
38
|
+
## 在线尝试
|
|
39
|
+
[codesandbox](https://codesandbox.io/p/sandbox/4v5slw)
|
|
7
40
|
|
|
8
|
-
- [x] token 解析
|
|
9
|
-
- [x] ast 解析
|
|
10
|
-
- [x] 生成数据
|
|
11
|
-
- [ ] 添加单测
|
|
12
|
-
- [ ] 新增主页
|
package/dist/es/index.js
CHANGED
|
@@ -2,201 +2,134 @@
|
|
|
2
2
|
function createParser() {
|
|
3
3
|
const root = {
|
|
4
4
|
type: "Root",
|
|
5
|
-
|
|
5
|
+
current: null
|
|
6
6
|
};
|
|
7
7
|
let currentRef = root;
|
|
8
|
-
const
|
|
8
|
+
const tryGetNextValue = (token) => {
|
|
9
|
+
if (token.type === "Keyword") {
|
|
10
|
+
return {
|
|
11
|
+
type: "Literal",
|
|
12
|
+
value: token.value
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
if (token.type === "String") {
|
|
16
|
+
return {
|
|
17
|
+
type: "Literal",
|
|
18
|
+
value: token.value
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
if (token.type === "Number") {
|
|
22
|
+
return {
|
|
23
|
+
type: "Literal",
|
|
24
|
+
value: token.value
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
if (token.type === "Symbol" && token.value === "[") {
|
|
28
|
+
return {
|
|
29
|
+
type: "Array",
|
|
30
|
+
children: [],
|
|
31
|
+
state: 0 /* Start */,
|
|
32
|
+
parent: currentRef,
|
|
33
|
+
current: null
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (token.type === "Symbol" && token.value === "{") {
|
|
37
|
+
return {
|
|
38
|
+
type: "Object",
|
|
39
|
+
children: [],
|
|
40
|
+
state: 0 /* Start */,
|
|
41
|
+
parent: currentRef,
|
|
42
|
+
current: null
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
};
|
|
47
|
+
const onArrayOrObjectFinish = () => {
|
|
48
|
+
currentRef = currentRef;
|
|
49
|
+
switch (currentRef.parent.type) {
|
|
50
|
+
case "Root":
|
|
51
|
+
return;
|
|
52
|
+
case "Array":
|
|
53
|
+
currentRef.parent.children.push(currentRef);
|
|
54
|
+
currentRef.parent.current = null;
|
|
55
|
+
currentRef = currentRef.parent;
|
|
56
|
+
return;
|
|
57
|
+
case "Property":
|
|
58
|
+
currentRef.parent.current = currentRef;
|
|
59
|
+
currentRef = currentRef.parent;
|
|
60
|
+
currentRef.parent.children.push(currentRef);
|
|
61
|
+
currentRef.parent.current = null;
|
|
62
|
+
currentRef = currentRef.parent;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const onPropertyFinish = () => {
|
|
67
|
+
currentRef = currentRef;
|
|
68
|
+
currentRef.parent.children.push(currentRef);
|
|
69
|
+
currentRef.parent.current = null;
|
|
70
|
+
currentRef = currentRef.parent;
|
|
71
|
+
};
|
|
72
|
+
const getRoot = () => root.current;
|
|
9
73
|
const write = (token) => {
|
|
10
74
|
switch (currentRef.type) {
|
|
11
75
|
case "Root":
|
|
12
|
-
if (currentRef.
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
20
|
-
if (token.type === "String") {
|
|
21
|
-
currentRef.value = {
|
|
22
|
-
type: "Literal",
|
|
23
|
-
value: token.value
|
|
24
|
-
};
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
if (token.type === "Number") {
|
|
28
|
-
currentRef.value = {
|
|
29
|
-
type: "Literal",
|
|
30
|
-
value: token.value
|
|
31
|
-
};
|
|
32
|
-
return;
|
|
33
|
-
}
|
|
34
|
-
if (token.type === "Symbol" && token.value === "[") {
|
|
35
|
-
currentRef.value = {
|
|
36
|
-
type: "Array",
|
|
37
|
-
children: [],
|
|
38
|
-
state: 0 /* Start */,
|
|
39
|
-
parent: currentRef,
|
|
40
|
-
current: null
|
|
41
|
-
};
|
|
42
|
-
currentRef = currentRef.value;
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
if (token.type === "Symbol" && token.value === "{") {
|
|
46
|
-
currentRef.value = {
|
|
47
|
-
type: "Object",
|
|
48
|
-
children: [],
|
|
49
|
-
state: 0 /* Start */,
|
|
50
|
-
parent: currentRef,
|
|
51
|
-
current: null
|
|
52
|
-
};
|
|
53
|
-
currentRef = currentRef.value;
|
|
76
|
+
if (currentRef.current === null) {
|
|
77
|
+
const next = tryGetNextValue(token);
|
|
78
|
+
if (next) {
|
|
79
|
+
currentRef.current = next;
|
|
80
|
+
if (next.type !== "Literal") {
|
|
81
|
+
currentRef = next;
|
|
82
|
+
}
|
|
54
83
|
return;
|
|
55
84
|
}
|
|
56
85
|
}
|
|
57
86
|
throw new Error("解析错误");
|
|
58
87
|
case "Array":
|
|
59
88
|
switch (currentRef.state) {
|
|
60
|
-
case 0 /* Start */:
|
|
89
|
+
case 0 /* Start */: {
|
|
61
90
|
if (token.type === "Symbol" && token.value === "]") {
|
|
62
|
-
|
|
63
|
-
case "Root":
|
|
64
|
-
return;
|
|
65
|
-
case "Array":
|
|
66
|
-
currentRef.parent.children.push(currentRef);
|
|
67
|
-
currentRef.parent.current = null;
|
|
68
|
-
currentRef = currentRef.parent;
|
|
69
|
-
return;
|
|
70
|
-
case "Property":
|
|
71
|
-
currentRef.parent.value = currentRef;
|
|
72
|
-
currentRef = currentRef.parent;
|
|
73
|
-
currentRef.parent.children.push(currentRef);
|
|
74
|
-
currentRef.parent.current = null;
|
|
75
|
-
currentRef = currentRef.parent;
|
|
76
|
-
return;
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
if (token.type === "Keyword") {
|
|
80
|
-
currentRef.state = 1 /* Value */;
|
|
81
|
-
currentRef.children.push({
|
|
82
|
-
type: "Literal",
|
|
83
|
-
value: token.value
|
|
84
|
-
});
|
|
85
|
-
return;
|
|
86
|
-
}
|
|
87
|
-
if (token.type === "String") {
|
|
88
|
-
currentRef.state = 1 /* Value */;
|
|
89
|
-
currentRef.children.push({
|
|
90
|
-
type: "Literal",
|
|
91
|
-
value: token.value
|
|
92
|
-
});
|
|
93
|
-
return;
|
|
94
|
-
}
|
|
95
|
-
if (token.type === "Number") {
|
|
96
|
-
currentRef.state = 1 /* Value */;
|
|
97
|
-
currentRef.children.push({
|
|
98
|
-
type: "Literal",
|
|
99
|
-
value: token.value
|
|
100
|
-
});
|
|
101
|
-
return;
|
|
102
|
-
}
|
|
103
|
-
if (token.type === "Symbol" && token.value === "[") {
|
|
104
|
-
currentRef.state = 1 /* Value */;
|
|
105
|
-
currentRef.current = {
|
|
106
|
-
type: "Array",
|
|
107
|
-
children: [],
|
|
108
|
-
state: 0 /* Start */,
|
|
109
|
-
parent: currentRef,
|
|
110
|
-
current: null
|
|
111
|
-
};
|
|
112
|
-
currentRef = currentRef.current;
|
|
91
|
+
onArrayOrObjectFinish();
|
|
113
92
|
return;
|
|
114
93
|
}
|
|
115
|
-
|
|
94
|
+
const next = tryGetNextValue(token);
|
|
95
|
+
if (next) {
|
|
116
96
|
currentRef.state = 1 /* Value */;
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
}
|
|
97
|
+
if (next.type === "Literal") {
|
|
98
|
+
currentRef.children.push(next);
|
|
99
|
+
currentRef.current = null;
|
|
100
|
+
} else {
|
|
101
|
+
currentRef.current = next;
|
|
102
|
+
currentRef = next;
|
|
103
|
+
}
|
|
124
104
|
return;
|
|
125
105
|
}
|
|
126
106
|
throw new Error("解析错误");
|
|
107
|
+
}
|
|
127
108
|
case 1 /* Value */:
|
|
128
109
|
if (token.type === "Symbol" && token.value === "]") {
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
return;
|
|
132
|
-
case "Array":
|
|
133
|
-
currentRef.parent.children.push(currentRef);
|
|
134
|
-
currentRef.parent.current = null;
|
|
135
|
-
currentRef = currentRef.parent;
|
|
136
|
-
return;
|
|
137
|
-
case "Property":
|
|
138
|
-
currentRef.parent.value = currentRef;
|
|
139
|
-
currentRef = currentRef.parent;
|
|
140
|
-
currentRef.parent.children.push(currentRef);
|
|
141
|
-
currentRef.parent.current = null;
|
|
142
|
-
currentRef = currentRef.parent;
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
110
|
+
onArrayOrObjectFinish();
|
|
111
|
+
return;
|
|
145
112
|
}
|
|
146
113
|
if (token.type === "Symbol" && token.value === ",") {
|
|
147
114
|
currentRef.state = 2 /* Comma */;
|
|
148
115
|
return;
|
|
149
116
|
}
|
|
150
117
|
throw new Error("解析错误");
|
|
151
|
-
case 2 /* Comma */:
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
currentRef.children.push({
|
|
155
|
-
type: "Literal",
|
|
156
|
-
value: token.value
|
|
157
|
-
});
|
|
158
|
-
return;
|
|
159
|
-
}
|
|
160
|
-
if (token.type === "String") {
|
|
161
|
-
currentRef.state = 1 /* Value */;
|
|
162
|
-
currentRef.children.push({
|
|
163
|
-
type: "Literal",
|
|
164
|
-
value: token.value
|
|
165
|
-
});
|
|
166
|
-
return;
|
|
167
|
-
}
|
|
168
|
-
if (token.type === "Number") {
|
|
169
|
-
currentRef.state = 1 /* Value */;
|
|
170
|
-
currentRef.children.push({
|
|
171
|
-
type: "Literal",
|
|
172
|
-
value: token.value
|
|
173
|
-
});
|
|
174
|
-
return;
|
|
175
|
-
}
|
|
176
|
-
if (token.type === "Symbol" && token.value === "[") {
|
|
118
|
+
case 2 /* Comma */: {
|
|
119
|
+
const next = tryGetNextValue(token);
|
|
120
|
+
if (next) {
|
|
177
121
|
currentRef.state = 1 /* Value */;
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
}
|
|
185
|
-
currentRef = currentRef.current;
|
|
186
|
-
return;
|
|
187
|
-
}
|
|
188
|
-
if (token.type === "Symbol" && token.value === "{") {
|
|
189
|
-
currentRef.state = 1 /* Value */;
|
|
190
|
-
currentRef.current = {
|
|
191
|
-
type: "Object",
|
|
192
|
-
children: [],
|
|
193
|
-
state: 0 /* Start */,
|
|
194
|
-
parent: currentRef,
|
|
195
|
-
current: null
|
|
196
|
-
};
|
|
122
|
+
if (next.type === "Literal") {
|
|
123
|
+
currentRef.children.push(next);
|
|
124
|
+
currentRef.current = null;
|
|
125
|
+
} else {
|
|
126
|
+
currentRef.current = next;
|
|
127
|
+
currentRef = next;
|
|
128
|
+
}
|
|
197
129
|
return;
|
|
198
130
|
}
|
|
199
131
|
throw new Error("解析错误");
|
|
132
|
+
}
|
|
200
133
|
}
|
|
201
134
|
case "Property":
|
|
202
135
|
switch (currentRef.state) {
|
|
@@ -206,65 +139,20 @@ function createParser() {
|
|
|
206
139
|
return;
|
|
207
140
|
}
|
|
208
141
|
throw new Error("解析错误");
|
|
209
|
-
case 1 /* Colon */:
|
|
210
|
-
|
|
142
|
+
case 1 /* Colon */: {
|
|
143
|
+
const next = tryGetNextValue(token);
|
|
144
|
+
if (next) {
|
|
211
145
|
currentRef.state = 2 /* Value */;
|
|
212
|
-
currentRef.
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
currentRef = currentRef.parent;
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
if (token.type === "String") {
|
|
222
|
-
currentRef.state = 2 /* Value */;
|
|
223
|
-
currentRef.value = {
|
|
224
|
-
type: "Literal",
|
|
225
|
-
value: token.value
|
|
226
|
-
};
|
|
227
|
-
currentRef.parent.children.push(currentRef);
|
|
228
|
-
currentRef.parent.current = null;
|
|
229
|
-
currentRef = currentRef.parent;
|
|
230
|
-
return;
|
|
231
|
-
}
|
|
232
|
-
if (token.type === "Number") {
|
|
233
|
-
currentRef.state = 2 /* Value */;
|
|
234
|
-
currentRef.value = {
|
|
235
|
-
type: "Literal",
|
|
236
|
-
value: token.value
|
|
237
|
-
};
|
|
238
|
-
currentRef.parent.children.push(currentRef);
|
|
239
|
-
currentRef.parent.current = null;
|
|
240
|
-
currentRef = currentRef.parent;
|
|
241
|
-
return;
|
|
242
|
-
}
|
|
243
|
-
if (token.type === "Symbol" && token.value === "[") {
|
|
244
|
-
currentRef.state = 2 /* Value */;
|
|
245
|
-
currentRef.value = {
|
|
246
|
-
type: "Array",
|
|
247
|
-
children: [],
|
|
248
|
-
state: 0 /* Start */,
|
|
249
|
-
parent: currentRef,
|
|
250
|
-
current: null
|
|
251
|
-
};
|
|
252
|
-
currentRef = currentRef.value;
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
if (token.type === "Symbol" && token.value === "{") {
|
|
256
|
-
currentRef.state = 2 /* Value */;
|
|
257
|
-
currentRef.value = {
|
|
258
|
-
type: "Object",
|
|
259
|
-
children: [],
|
|
260
|
-
state: 0 /* Start */,
|
|
261
|
-
parent: currentRef,
|
|
262
|
-
current: null
|
|
263
|
-
};
|
|
264
|
-
currentRef = currentRef.value;
|
|
146
|
+
currentRef.current = next;
|
|
147
|
+
if (next.type === "Literal") {
|
|
148
|
+
onPropertyFinish();
|
|
149
|
+
} else {
|
|
150
|
+
currentRef = next;
|
|
151
|
+
}
|
|
265
152
|
return;
|
|
266
153
|
}
|
|
267
154
|
throw new Error("解析错误");
|
|
155
|
+
}
|
|
268
156
|
}
|
|
269
157
|
case "Object":
|
|
270
158
|
switch (currentRef.state) {
|
|
@@ -277,7 +165,7 @@ function createParser() {
|
|
|
277
165
|
type: "Identifier",
|
|
278
166
|
value: token.value
|
|
279
167
|
},
|
|
280
|
-
|
|
168
|
+
current: null,
|
|
281
169
|
state: 0 /* Key */,
|
|
282
170
|
parent: currentRef
|
|
283
171
|
};
|
|
@@ -285,42 +173,14 @@ function createParser() {
|
|
|
285
173
|
return;
|
|
286
174
|
}
|
|
287
175
|
if (token.type === "Symbol" && token.value === "}") {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
return;
|
|
291
|
-
case "Array":
|
|
292
|
-
currentRef.parent.children.push(currentRef);
|
|
293
|
-
currentRef.parent.current = null;
|
|
294
|
-
currentRef = currentRef.parent;
|
|
295
|
-
return;
|
|
296
|
-
case "Property":
|
|
297
|
-
currentRef.parent.value = currentRef;
|
|
298
|
-
currentRef = currentRef.parent;
|
|
299
|
-
currentRef.parent.children.push(currentRef);
|
|
300
|
-
currentRef.parent.current = null;
|
|
301
|
-
currentRef = currentRef.parent;
|
|
302
|
-
return;
|
|
303
|
-
}
|
|
176
|
+
onArrayOrObjectFinish();
|
|
177
|
+
return;
|
|
304
178
|
}
|
|
305
179
|
throw new Error("解析错误");
|
|
306
180
|
case 1 /* Property */:
|
|
307
181
|
if (token.type === "Symbol" && token.value === "}") {
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
return;
|
|
311
|
-
case "Array":
|
|
312
|
-
currentRef.parent.children.push(currentRef);
|
|
313
|
-
currentRef.parent.current = null;
|
|
314
|
-
currentRef = currentRef.parent;
|
|
315
|
-
return;
|
|
316
|
-
case "Property":
|
|
317
|
-
currentRef.parent.value = currentRef;
|
|
318
|
-
currentRef = currentRef.parent;
|
|
319
|
-
currentRef.parent.children.push(currentRef);
|
|
320
|
-
currentRef.parent.current = null;
|
|
321
|
-
currentRef = currentRef.parent;
|
|
322
|
-
return;
|
|
323
|
-
}
|
|
182
|
+
onArrayOrObjectFinish();
|
|
183
|
+
return;
|
|
324
184
|
}
|
|
325
185
|
if (token.type === "Symbol" && token.value === ",") {
|
|
326
186
|
currentRef.state = 2 /* Comma */;
|
|
@@ -336,7 +196,7 @@ function createParser() {
|
|
|
336
196
|
type: "Identifier",
|
|
337
197
|
value: token.value
|
|
338
198
|
},
|
|
339
|
-
|
|
199
|
+
current: null,
|
|
340
200
|
state: 0 /* Key */,
|
|
341
201
|
parent: currentRef
|
|
342
202
|
};
|
|
@@ -379,7 +239,7 @@ function createTokenize() {
|
|
|
379
239
|
if (current) {
|
|
380
240
|
switch (current.type) {
|
|
381
241
|
case "Keyword":
|
|
382
|
-
if (char ===
|
|
242
|
+
if (char === current.value[current.matchedIndex + 1]) {
|
|
383
243
|
current.matchedIndex++;
|
|
384
244
|
if (current.matchedIndex === current.value.length - 1) {
|
|
385
245
|
const token = {
|
|
@@ -709,8 +569,8 @@ function getValueFromNode(node) {
|
|
|
709
569
|
case "Object":
|
|
710
570
|
return (node.current ? [...node.children, node.current] : node.children).reduce(
|
|
711
571
|
(obj, property) => {
|
|
712
|
-
if (property.
|
|
713
|
-
obj[property.key.value] = getValueFromNode(property.
|
|
572
|
+
if (property.current) {
|
|
573
|
+
obj[property.key.value] = getValueFromNode(property.current);
|
|
714
574
|
}
|
|
715
575
|
return obj;
|
|
716
576
|
},
|