@loaders.gl/json 3.1.0-alpha.3 → 4.0.0-alpha.4
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/geojson-loader.js +1 -1
- package/dist/geojson-worker.js +1702 -2
- package/dist/json-loader.js +1 -1
- package/dist/lib/clarinet/clarinet.js +222 -260
- package/dist/lib/clarinet/clarinet.js.map +1 -1
- package/dist/lib/parse-ndjson-in-batches.js +1 -1
- package/dist/lib/parse-ndjson-in-batches.js.map +1 -1
- package/dist/lib/parse-ndjson.js +1 -1
- package/dist/lib/parse-ndjson.js.map +1 -1
- package/dist/lib/parser/json-parser.js +48 -47
- package/dist/lib/parser/json-parser.js.map +1 -1
- package/dist/lib/parser/streaming-json-parser.js +29 -34
- package/dist/lib/parser/streaming-json-parser.js.map +1 -1
- package/dist/ndjson-loader.js +3 -1
- package/dist/ndjson-loader.js.map +1 -1
- package/package.json +7 -7
- package/src/lib/clarinet/clarinet.ts +539 -0
- package/src/lib/parser/json-parser.ts +52 -55
- package/src/lib/parser/streaming-json-parser.ts +28 -32
- package/src/ndjson-loader.ts +3 -1
- package/dist/dist.min.js +0 -2
- package/dist/dist.min.js.map +0 -1
- package/dist/geojson-worker.js.map +0 -1
- package/src/lib/clarinet/clarinet.js +0 -578
|
@@ -0,0 +1,539 @@
|
|
|
1
|
+
// loaders.gl, MIT license
|
|
2
|
+
// This is a fork of the clarinet library, originally BSD license (see LICENSE file)
|
|
3
|
+
// loaders.gl changes:
|
|
4
|
+
// - typescript port
|
|
5
|
+
|
|
6
|
+
export type ClarinetEvent =
|
|
7
|
+
| 'onvalue'
|
|
8
|
+
| 'onstring'
|
|
9
|
+
| 'onkey'
|
|
10
|
+
| 'onopenobject'
|
|
11
|
+
| 'oncloseobject'
|
|
12
|
+
| 'onopenarray'
|
|
13
|
+
| 'onclosearray'
|
|
14
|
+
| 'onerror'
|
|
15
|
+
| 'onend'
|
|
16
|
+
| 'onready';
|
|
17
|
+
|
|
18
|
+
// Removes the MAX_BUFFER_LENGTH, originally set to 64 * 1024
|
|
19
|
+
const MAX_BUFFER_LENGTH = Number.MAX_SAFE_INTEGER;
|
|
20
|
+
// const DEBUG = false;
|
|
21
|
+
|
|
22
|
+
enum STATE {
|
|
23
|
+
BEGIN = 0,
|
|
24
|
+
VALUE, // general stuff
|
|
25
|
+
OPEN_OBJECT, // {
|
|
26
|
+
CLOSE_OBJECT, // }
|
|
27
|
+
OPEN_ARRAY, // [
|
|
28
|
+
CLOSE_ARRAY, // ]
|
|
29
|
+
TEXT_ESCAPE, // \ stuff
|
|
30
|
+
STRING, // ""
|
|
31
|
+
BACKSLASH,
|
|
32
|
+
END, // No more stack
|
|
33
|
+
OPEN_KEY, // , "a"
|
|
34
|
+
CLOSE_KEY, // :
|
|
35
|
+
TRUE, // r
|
|
36
|
+
TRUE2, // u
|
|
37
|
+
TRUE3, // e
|
|
38
|
+
FALSE, // a
|
|
39
|
+
FALSE2, // l
|
|
40
|
+
FALSE3, // s
|
|
41
|
+
FALSE4, // e
|
|
42
|
+
NULL, // u
|
|
43
|
+
NULL2, // l
|
|
44
|
+
NULL3, // l
|
|
45
|
+
NUMBER_DECIMAL_POINT, // .
|
|
46
|
+
NUMBER_DIGIT // [0-9]
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const Char = {
|
|
50
|
+
tab: 0x09, // \t
|
|
51
|
+
lineFeed: 0x0a, // \n
|
|
52
|
+
carriageReturn: 0x0d, // \r
|
|
53
|
+
space: 0x20, // " "
|
|
54
|
+
|
|
55
|
+
doubleQuote: 0x22, // "
|
|
56
|
+
plus: 0x2b, // +
|
|
57
|
+
comma: 0x2c, // ,
|
|
58
|
+
minus: 0x2d, // -
|
|
59
|
+
period: 0x2e, // .
|
|
60
|
+
|
|
61
|
+
_0: 0x30, // 0
|
|
62
|
+
_9: 0x39, // 9
|
|
63
|
+
|
|
64
|
+
colon: 0x3a, // :
|
|
65
|
+
|
|
66
|
+
E: 0x45, // E
|
|
67
|
+
|
|
68
|
+
openBracket: 0x5b, // [
|
|
69
|
+
backslash: 0x5c, // \
|
|
70
|
+
closeBracket: 0x5d, // ]
|
|
71
|
+
|
|
72
|
+
a: 0x61, // a
|
|
73
|
+
b: 0x62, // b
|
|
74
|
+
e: 0x65, // e
|
|
75
|
+
f: 0x66, // f
|
|
76
|
+
l: 0x6c, // l
|
|
77
|
+
n: 0x6e, // n
|
|
78
|
+
r: 0x72, // r
|
|
79
|
+
s: 0x73, // s
|
|
80
|
+
t: 0x74, // t
|
|
81
|
+
u: 0x75, // u
|
|
82
|
+
|
|
83
|
+
openBrace: 0x7b, // {
|
|
84
|
+
closeBrace: 0x7d // }
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const stringTokenPattern = /[\\"\n]/g;
|
|
88
|
+
|
|
89
|
+
type ParserEvent = (parser: ClarinetParser, event: string, data?: any) => void;
|
|
90
|
+
|
|
91
|
+
export type ClarinetParserOptions = {
|
|
92
|
+
onready?: ParserEvent;
|
|
93
|
+
onopenobject?: ParserEvent;
|
|
94
|
+
onkey?: ParserEvent;
|
|
95
|
+
oncloseobject?: ParserEvent;
|
|
96
|
+
onopenarray?: ParserEvent;
|
|
97
|
+
onclosearray?: ParserEvent;
|
|
98
|
+
onvalue?: ParserEvent;
|
|
99
|
+
onerror?: ParserEvent;
|
|
100
|
+
onend?: ParserEvent;
|
|
101
|
+
onchunkparsed?: ParserEvent;
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
const DEFAULT_OPTIONS: Required<ClarinetParserOptions> = {
|
|
105
|
+
onready: () => {},
|
|
106
|
+
onopenobject: () => {},
|
|
107
|
+
onkey: () => {},
|
|
108
|
+
oncloseobject: () => {},
|
|
109
|
+
onopenarray: () => {},
|
|
110
|
+
onclosearray: () => {},
|
|
111
|
+
onvalue: () => {},
|
|
112
|
+
onerror: () => {},
|
|
113
|
+
onend: () => {},
|
|
114
|
+
onchunkparsed: () => {}
|
|
115
|
+
};
|
|
116
|
+
export default class ClarinetParser {
|
|
117
|
+
protected options: Required<ClarinetParserOptions> = DEFAULT_OPTIONS;
|
|
118
|
+
|
|
119
|
+
bufferCheckPosition = MAX_BUFFER_LENGTH;
|
|
120
|
+
q = '';
|
|
121
|
+
c = '';
|
|
122
|
+
p = '';
|
|
123
|
+
closed = false;
|
|
124
|
+
closedRoot = false;
|
|
125
|
+
sawRoot = false;
|
|
126
|
+
// tag = null;
|
|
127
|
+
error: Error | null = null;
|
|
128
|
+
state = STATE.BEGIN;
|
|
129
|
+
stack: STATE[] = [];
|
|
130
|
+
// mostly just for error reporting
|
|
131
|
+
position: number = 0;
|
|
132
|
+
column: number = 0;
|
|
133
|
+
line: number = 1;
|
|
134
|
+
slashed: boolean = false;
|
|
135
|
+
unicodeI: number = 0;
|
|
136
|
+
unicodeS: string | null = null;
|
|
137
|
+
depth: number = 0;
|
|
138
|
+
|
|
139
|
+
textNode;
|
|
140
|
+
numberNode;
|
|
141
|
+
|
|
142
|
+
constructor(options: ClarinetParserOptions = {}) {
|
|
143
|
+
this.options = {...DEFAULT_OPTIONS, ...options};
|
|
144
|
+
this.textNode = undefined;
|
|
145
|
+
this.numberNode = '';
|
|
146
|
+
this.emit('onready');
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
end() {
|
|
150
|
+
if (this.state !== STATE.VALUE || this.depth !== 0) this._error('Unexpected end');
|
|
151
|
+
|
|
152
|
+
this._closeValue();
|
|
153
|
+
this.c = '';
|
|
154
|
+
this.closed = true;
|
|
155
|
+
this.emit('onend');
|
|
156
|
+
return this;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
resume() {
|
|
160
|
+
this.error = null;
|
|
161
|
+
return this;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
close() {
|
|
165
|
+
return this.write(null);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// protected
|
|
169
|
+
|
|
170
|
+
emit(event: string, data?: any): void {
|
|
171
|
+
// if (DEBUG) console.log('-- emit', event, data);
|
|
172
|
+
this.options[event]?.(data, this);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
emitNode(event: string, data?: any): void {
|
|
176
|
+
this._closeValue();
|
|
177
|
+
this.emit(event, data);
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/* eslint-disable no-continue */
|
|
181
|
+
// eslint-disable-next-line complexity, max-statements
|
|
182
|
+
write(chunk) {
|
|
183
|
+
if (this.error) {
|
|
184
|
+
throw this.error;
|
|
185
|
+
}
|
|
186
|
+
if (this.closed) {
|
|
187
|
+
return this._error('Cannot write after close. Assign an onready handler.');
|
|
188
|
+
}
|
|
189
|
+
if (chunk === null) {
|
|
190
|
+
return this.end();
|
|
191
|
+
}
|
|
192
|
+
let i = 0;
|
|
193
|
+
let c = chunk.charCodeAt(0);
|
|
194
|
+
let p = this.p;
|
|
195
|
+
// if (DEBUG) console.log(`write -> [${ chunk }]`);
|
|
196
|
+
while (c) {
|
|
197
|
+
p = c;
|
|
198
|
+
this.c = c = chunk.charCodeAt(i++);
|
|
199
|
+
// if chunk doesnt have next, like streaming char by char
|
|
200
|
+
// this way we need to check if previous is really previous
|
|
201
|
+
// if not we need to reset to what the this says is the previous
|
|
202
|
+
// from buffer
|
|
203
|
+
if (p !== c) {
|
|
204
|
+
this.p = p;
|
|
205
|
+
} else {
|
|
206
|
+
p = this.p;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
if (!c) break;
|
|
210
|
+
|
|
211
|
+
// if (DEBUG) console.log(i, c, STATE[this.state]);
|
|
212
|
+
this.position++;
|
|
213
|
+
if (c === Char.lineFeed) {
|
|
214
|
+
this.line++;
|
|
215
|
+
this.column = 0;
|
|
216
|
+
} else this.column++;
|
|
217
|
+
|
|
218
|
+
switch (this.state) {
|
|
219
|
+
case STATE.BEGIN:
|
|
220
|
+
if (c === Char.openBrace) this.state = STATE.OPEN_OBJECT;
|
|
221
|
+
else if (c === Char.openBracket) this.state = STATE.OPEN_ARRAY;
|
|
222
|
+
else if (!isWhitespace(c)) {
|
|
223
|
+
this._error('Non-whitespace before {[.');
|
|
224
|
+
}
|
|
225
|
+
continue;
|
|
226
|
+
|
|
227
|
+
case STATE.OPEN_KEY:
|
|
228
|
+
case STATE.OPEN_OBJECT:
|
|
229
|
+
if (isWhitespace(c)) continue;
|
|
230
|
+
if (this.state === STATE.OPEN_KEY) this.stack.push(STATE.CLOSE_KEY);
|
|
231
|
+
else if (c === Char.closeBrace) {
|
|
232
|
+
this.emit('onopenobject');
|
|
233
|
+
this.depth++;
|
|
234
|
+
this.emit('oncloseobject');
|
|
235
|
+
this.depth--;
|
|
236
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
237
|
+
continue;
|
|
238
|
+
} else this.stack.push(STATE.CLOSE_OBJECT);
|
|
239
|
+
if (c === Char.doubleQuote) this.state = STATE.STRING;
|
|
240
|
+
else this._error('Malformed object key should start with "');
|
|
241
|
+
continue;
|
|
242
|
+
|
|
243
|
+
case STATE.CLOSE_KEY:
|
|
244
|
+
case STATE.CLOSE_OBJECT:
|
|
245
|
+
if (isWhitespace(c)) continue;
|
|
246
|
+
// let event = this.state === STATE.CLOSE_KEY ? 'key' : 'object';
|
|
247
|
+
if (c === Char.colon) {
|
|
248
|
+
if (this.state === STATE.CLOSE_OBJECT) {
|
|
249
|
+
this.stack.push(STATE.CLOSE_OBJECT);
|
|
250
|
+
this._closeValue('onopenobject');
|
|
251
|
+
this.depth++;
|
|
252
|
+
} else this._closeValue('onkey');
|
|
253
|
+
this.state = STATE.VALUE;
|
|
254
|
+
} else if (c === Char.closeBrace) {
|
|
255
|
+
this.emitNode('oncloseobject');
|
|
256
|
+
this.depth--;
|
|
257
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
258
|
+
} else if (c === Char.comma) {
|
|
259
|
+
if (this.state === STATE.CLOSE_OBJECT) this.stack.push(STATE.CLOSE_OBJECT);
|
|
260
|
+
this._closeValue();
|
|
261
|
+
this.state = STATE.OPEN_KEY;
|
|
262
|
+
} else this._error('Bad object');
|
|
263
|
+
continue;
|
|
264
|
+
|
|
265
|
+
case STATE.OPEN_ARRAY: // after an array there always a value
|
|
266
|
+
case STATE.VALUE:
|
|
267
|
+
if (isWhitespace(c)) continue;
|
|
268
|
+
if (this.state === STATE.OPEN_ARRAY) {
|
|
269
|
+
this.emit('onopenarray');
|
|
270
|
+
this.depth++;
|
|
271
|
+
this.state = STATE.VALUE;
|
|
272
|
+
if (c === Char.closeBracket) {
|
|
273
|
+
this.emit('onclosearray');
|
|
274
|
+
this.depth--;
|
|
275
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
276
|
+
continue;
|
|
277
|
+
} else {
|
|
278
|
+
this.stack.push(STATE.CLOSE_ARRAY);
|
|
279
|
+
}
|
|
280
|
+
}
|
|
281
|
+
if (c === Char.doubleQuote) this.state = STATE.STRING;
|
|
282
|
+
else if (c === Char.openBrace) this.state = STATE.OPEN_OBJECT;
|
|
283
|
+
else if (c === Char.openBracket) this.state = STATE.OPEN_ARRAY;
|
|
284
|
+
else if (c === Char.t) this.state = STATE.TRUE;
|
|
285
|
+
else if (c === Char.f) this.state = STATE.FALSE;
|
|
286
|
+
else if (c === Char.n) this.state = STATE.NULL;
|
|
287
|
+
else if (c === Char.minus) {
|
|
288
|
+
// keep and continue
|
|
289
|
+
this.numberNode += '-';
|
|
290
|
+
} else if (Char._0 <= c && c <= Char._9) {
|
|
291
|
+
this.numberNode += String.fromCharCode(c);
|
|
292
|
+
this.state = STATE.NUMBER_DIGIT;
|
|
293
|
+
} else this._error('Bad value');
|
|
294
|
+
continue;
|
|
295
|
+
|
|
296
|
+
case STATE.CLOSE_ARRAY:
|
|
297
|
+
if (c === Char.comma) {
|
|
298
|
+
this.stack.push(STATE.CLOSE_ARRAY);
|
|
299
|
+
this._closeValue('onvalue');
|
|
300
|
+
this.state = STATE.VALUE;
|
|
301
|
+
} else if (c === Char.closeBracket) {
|
|
302
|
+
this.emitNode('onclosearray');
|
|
303
|
+
this.depth--;
|
|
304
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
305
|
+
} else if (isWhitespace(c)) continue;
|
|
306
|
+
else this._error('Bad array');
|
|
307
|
+
continue;
|
|
308
|
+
|
|
309
|
+
case STATE.STRING:
|
|
310
|
+
if (this.textNode === undefined) {
|
|
311
|
+
this.textNode = '';
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// thanks thejh, this is an about 50% performance improvement.
|
|
315
|
+
let starti = i - 1;
|
|
316
|
+
let slashed = this.slashed;
|
|
317
|
+
let unicodeI = this.unicodeI;
|
|
318
|
+
// eslint-disable-next-line no-constant-condition, no-labels
|
|
319
|
+
STRING_BIGLOOP: while (true) {
|
|
320
|
+
// if (DEBUG) console.log(i, c, STATE[this.state], slashed);
|
|
321
|
+
// zero means "no unicode active". 1-4 mean "parse some more". end after 4.
|
|
322
|
+
while (unicodeI > 0) {
|
|
323
|
+
this.unicodeS += String.fromCharCode(c);
|
|
324
|
+
c = chunk.charCodeAt(i++);
|
|
325
|
+
this.position++;
|
|
326
|
+
if (unicodeI === 4) {
|
|
327
|
+
// TODO this might be slow? well, probably not used too often anyway
|
|
328
|
+
this.textNode += String.fromCharCode(parseInt(this.unicodeS as string, 16));
|
|
329
|
+
unicodeI = 0;
|
|
330
|
+
starti = i - 1;
|
|
331
|
+
} else {
|
|
332
|
+
unicodeI++;
|
|
333
|
+
}
|
|
334
|
+
// we can just break here: no stuff we skipped that still has to be sliced out or so
|
|
335
|
+
// eslint-disable-next-line no-labels
|
|
336
|
+
if (!c) break STRING_BIGLOOP;
|
|
337
|
+
}
|
|
338
|
+
if (c === Char.doubleQuote && !slashed) {
|
|
339
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
340
|
+
this.textNode += chunk.substring(starti, i - 1);
|
|
341
|
+
this.position += i - 1 - starti;
|
|
342
|
+
break;
|
|
343
|
+
}
|
|
344
|
+
if (c === Char.backslash && !slashed) {
|
|
345
|
+
slashed = true;
|
|
346
|
+
this.textNode += chunk.substring(starti, i - 1);
|
|
347
|
+
this.position += i - 1 - starti;
|
|
348
|
+
c = chunk.charCodeAt(i++);
|
|
349
|
+
this.position++;
|
|
350
|
+
if (!c) break;
|
|
351
|
+
}
|
|
352
|
+
if (slashed) {
|
|
353
|
+
slashed = false;
|
|
354
|
+
if (c === Char.n) {
|
|
355
|
+
this.textNode += '\n';
|
|
356
|
+
} else if (c === Char.r) {
|
|
357
|
+
this.textNode += '\r';
|
|
358
|
+
} else if (c === Char.t) {
|
|
359
|
+
this.textNode += '\t';
|
|
360
|
+
} else if (c === Char.f) {
|
|
361
|
+
this.textNode += '\f';
|
|
362
|
+
} else if (c === Char.b) {
|
|
363
|
+
this.textNode += '\b';
|
|
364
|
+
} else if (c === Char.u) {
|
|
365
|
+
// \uxxxx. meh!
|
|
366
|
+
unicodeI = 1;
|
|
367
|
+
this.unicodeS = '';
|
|
368
|
+
} else {
|
|
369
|
+
this.textNode += String.fromCharCode(c);
|
|
370
|
+
}
|
|
371
|
+
c = chunk.charCodeAt(i++);
|
|
372
|
+
this.position++;
|
|
373
|
+
starti = i - 1;
|
|
374
|
+
if (!c) break;
|
|
375
|
+
else continue;
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
stringTokenPattern.lastIndex = i;
|
|
379
|
+
const reResult = stringTokenPattern.exec(chunk);
|
|
380
|
+
if (reResult === null) {
|
|
381
|
+
i = chunk.length + 1;
|
|
382
|
+
this.textNode += chunk.substring(starti, i - 1);
|
|
383
|
+
this.position += i - 1 - starti;
|
|
384
|
+
break;
|
|
385
|
+
}
|
|
386
|
+
i = reResult.index + 1;
|
|
387
|
+
c = chunk.charCodeAt(reResult.index);
|
|
388
|
+
if (!c) {
|
|
389
|
+
this.textNode += chunk.substring(starti, i - 1);
|
|
390
|
+
this.position += i - 1 - starti;
|
|
391
|
+
break;
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
this.slashed = slashed;
|
|
395
|
+
this.unicodeI = unicodeI;
|
|
396
|
+
continue;
|
|
397
|
+
|
|
398
|
+
case STATE.TRUE:
|
|
399
|
+
if (c === Char.r) this.state = STATE.TRUE2;
|
|
400
|
+
else this._error(`Invalid true started with t${c}`);
|
|
401
|
+
continue;
|
|
402
|
+
|
|
403
|
+
case STATE.TRUE2:
|
|
404
|
+
if (c === Char.u) this.state = STATE.TRUE3;
|
|
405
|
+
else this._error(`Invalid true started with tr${c}`);
|
|
406
|
+
continue;
|
|
407
|
+
|
|
408
|
+
case STATE.TRUE3:
|
|
409
|
+
if (c === Char.e) {
|
|
410
|
+
this.emit('onvalue', true);
|
|
411
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
412
|
+
} else this._error(`Invalid true started with tru${c}`);
|
|
413
|
+
continue;
|
|
414
|
+
|
|
415
|
+
case STATE.FALSE:
|
|
416
|
+
if (c === Char.a) this.state = STATE.FALSE2;
|
|
417
|
+
else this._error(`Invalid false started with f${c}`);
|
|
418
|
+
continue;
|
|
419
|
+
|
|
420
|
+
case STATE.FALSE2:
|
|
421
|
+
if (c === Char.l) this.state = STATE.FALSE3;
|
|
422
|
+
else this._error(`Invalid false started with fa${c}`);
|
|
423
|
+
continue;
|
|
424
|
+
|
|
425
|
+
case STATE.FALSE3:
|
|
426
|
+
if (c === Char.s) this.state = STATE.FALSE4;
|
|
427
|
+
else this._error(`Invalid false started with fal${c}`);
|
|
428
|
+
continue;
|
|
429
|
+
|
|
430
|
+
case STATE.FALSE4:
|
|
431
|
+
if (c === Char.e) {
|
|
432
|
+
this.emit('onvalue', false);
|
|
433
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
434
|
+
} else this._error(`Invalid false started with fals${c}`);
|
|
435
|
+
continue;
|
|
436
|
+
|
|
437
|
+
case STATE.NULL:
|
|
438
|
+
if (c === Char.u) this.state = STATE.NULL2;
|
|
439
|
+
else this._error(`Invalid null started with n${c}`);
|
|
440
|
+
continue;
|
|
441
|
+
|
|
442
|
+
case STATE.NULL2:
|
|
443
|
+
if (c === Char.l) this.state = STATE.NULL3;
|
|
444
|
+
else this._error(`Invalid null started with nu${c}`);
|
|
445
|
+
continue;
|
|
446
|
+
|
|
447
|
+
case STATE.NULL3:
|
|
448
|
+
if (c === Char.l) {
|
|
449
|
+
this.emit('onvalue', null);
|
|
450
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
451
|
+
} else this._error(`Invalid null started with nul${c}`);
|
|
452
|
+
continue;
|
|
453
|
+
|
|
454
|
+
case STATE.NUMBER_DECIMAL_POINT:
|
|
455
|
+
if (c === Char.period) {
|
|
456
|
+
this.numberNode += '.';
|
|
457
|
+
this.state = STATE.NUMBER_DIGIT;
|
|
458
|
+
} else this._error('Leading zero not followed by .');
|
|
459
|
+
continue;
|
|
460
|
+
|
|
461
|
+
case STATE.NUMBER_DIGIT:
|
|
462
|
+
if (Char._0 <= c && c <= Char._9) this.numberNode += String.fromCharCode(c);
|
|
463
|
+
else if (c === Char.period) {
|
|
464
|
+
if (this.numberNode.indexOf('.') !== -1) this._error('Invalid number has two dots');
|
|
465
|
+
this.numberNode += '.';
|
|
466
|
+
} else if (c === Char.e || c === Char.E) {
|
|
467
|
+
if (this.numberNode.indexOf('e') !== -1 || this.numberNode.indexOf('E') !== -1)
|
|
468
|
+
this._error('Invalid number has two exponential');
|
|
469
|
+
this.numberNode += 'e';
|
|
470
|
+
} else if (c === Char.plus || c === Char.minus) {
|
|
471
|
+
// @ts-expect-error
|
|
472
|
+
if (!(p === Char.e || p === Char.E)) this._error('Invalid symbol in number');
|
|
473
|
+
this.numberNode += String.fromCharCode(c);
|
|
474
|
+
} else {
|
|
475
|
+
this._closeNumber();
|
|
476
|
+
i--; // go back one
|
|
477
|
+
this.state = this.stack.pop() || STATE.VALUE;
|
|
478
|
+
}
|
|
479
|
+
continue;
|
|
480
|
+
|
|
481
|
+
default:
|
|
482
|
+
this._error(`Unknown state: ${this.state}`);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
if (this.position >= this.bufferCheckPosition) {
|
|
486
|
+
checkBufferLength(this);
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
this.emit('onchunkparsed');
|
|
490
|
+
|
|
491
|
+
return this;
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
_closeValue(event: string = 'onvalue'): void {
|
|
495
|
+
if (this.textNode !== undefined) {
|
|
496
|
+
this.emit(event, this.textNode);
|
|
497
|
+
}
|
|
498
|
+
this.textNode = undefined;
|
|
499
|
+
}
|
|
500
|
+
|
|
501
|
+
_closeNumber(): void {
|
|
502
|
+
if (this.numberNode) this.emit('onvalue', parseFloat(this.numberNode));
|
|
503
|
+
this.numberNode = '';
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
_error(message: string = ''): void {
|
|
507
|
+
this._closeValue();
|
|
508
|
+
message += `\nLine: ${this.line}\nColumn: ${this.column}\nChar: ${this.c}`;
|
|
509
|
+
const error = new Error(message);
|
|
510
|
+
this.error = error;
|
|
511
|
+
this.emit('onerror', error);
|
|
512
|
+
}
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
function isWhitespace(c): boolean {
|
|
516
|
+
return c === Char.carriageReturn || c === Char.lineFeed || c === Char.space || c === Char.tab;
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
function checkBufferLength(parser) {
|
|
520
|
+
const maxAllowed = Math.max(MAX_BUFFER_LENGTH, 10);
|
|
521
|
+
let maxActual = 0;
|
|
522
|
+
|
|
523
|
+
for (const buffer of ['textNode', 'numberNode']) {
|
|
524
|
+
const len = parser[buffer] === undefined ? 0 : parser[buffer].length;
|
|
525
|
+
if (len > maxAllowed) {
|
|
526
|
+
switch (buffer) {
|
|
527
|
+
case 'text':
|
|
528
|
+
// TODO - should this be closeValue?
|
|
529
|
+
// closeText(parser);
|
|
530
|
+
break;
|
|
531
|
+
|
|
532
|
+
default:
|
|
533
|
+
parser._error(`Max buffer length exceeded: ${buffer}`);
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
maxActual = Math.max(maxActual, len);
|
|
537
|
+
}
|
|
538
|
+
parser.bufferCheckPosition = MAX_BUFFER_LENGTH - maxActual + parser.position;
|
|
539
|
+
}
|
|
@@ -1,17 +1,64 @@
|
|
|
1
1
|
// @ts-nocheck
|
|
2
2
|
|
|
3
|
-
import ClarinetParser from '../clarinet/clarinet';
|
|
3
|
+
import ClarinetParser, {ClarinetParserOptions} from '../clarinet/clarinet';
|
|
4
4
|
import JSONPath from '../jsonpath/jsonpath';
|
|
5
5
|
|
|
6
6
|
// JSONParser builds a JSON object using the events emitted by the Clarinet parser
|
|
7
7
|
|
|
8
8
|
export default class JSONParser {
|
|
9
|
-
|
|
10
|
-
|
|
9
|
+
readonly parser: ClarinetParser;
|
|
10
|
+
result = undefined;
|
|
11
|
+
previousStates = [];
|
|
12
|
+
currentState = Object.freeze({container: [], key: null});
|
|
13
|
+
jsonpath: JSONPath = new JSONPath();
|
|
11
14
|
|
|
12
|
-
constructor() {
|
|
15
|
+
constructor(options: ClarinetParserOptions) {
|
|
13
16
|
this.reset();
|
|
14
|
-
this.
|
|
17
|
+
this.parser = new ClarinetParser({
|
|
18
|
+
onready: () => {
|
|
19
|
+
this.jsonpath = new JSONPath();
|
|
20
|
+
this.previousStates.length = 0;
|
|
21
|
+
this.currentState.container.length = 0;
|
|
22
|
+
},
|
|
23
|
+
|
|
24
|
+
onopenobject: (name) => {
|
|
25
|
+
this._openObject({});
|
|
26
|
+
if (typeof name !== 'undefined') {
|
|
27
|
+
this.parser.emit('onkey', name);
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
|
|
31
|
+
onkey: (name) => {
|
|
32
|
+
this.jsonpath.set(name);
|
|
33
|
+
this.currentState.key = name;
|
|
34
|
+
},
|
|
35
|
+
|
|
36
|
+
oncloseobject: () => {
|
|
37
|
+
this._closeObject();
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
onopenarray: () => {
|
|
41
|
+
this._openArray();
|
|
42
|
+
},
|
|
43
|
+
|
|
44
|
+
onclosearray: () => {
|
|
45
|
+
this._closeArray();
|
|
46
|
+
},
|
|
47
|
+
|
|
48
|
+
onvalue: (value) => {
|
|
49
|
+
this._pushOrSet(value);
|
|
50
|
+
},
|
|
51
|
+
|
|
52
|
+
onerror: (error) => {
|
|
53
|
+
throw error;
|
|
54
|
+
},
|
|
55
|
+
|
|
56
|
+
onend: () => {
|
|
57
|
+
this.result = this.currentState.container.pop();
|
|
58
|
+
},
|
|
59
|
+
|
|
60
|
+
...options
|
|
61
|
+
});
|
|
15
62
|
}
|
|
16
63
|
|
|
17
64
|
reset(): void {
|
|
@@ -64,54 +111,4 @@ export default class JSONParser {
|
|
|
64
111
|
this.jsonpath.pop();
|
|
65
112
|
this.currentState = this.previousStates.pop();
|
|
66
113
|
}
|
|
67
|
-
|
|
68
|
-
_initializeParser(): void {
|
|
69
|
-
this._parser = new ClarinetParser({
|
|
70
|
-
onready: () => {
|
|
71
|
-
this.jsonpath = new JSONPath();
|
|
72
|
-
this.previousStates.length = 0;
|
|
73
|
-
this.currentState.container.length = 0;
|
|
74
|
-
},
|
|
75
|
-
|
|
76
|
-
onopenobject: (name) => {
|
|
77
|
-
this._openObject({});
|
|
78
|
-
if (typeof name !== 'undefined') {
|
|
79
|
-
this.parser.onkey(name);
|
|
80
|
-
}
|
|
81
|
-
},
|
|
82
|
-
|
|
83
|
-
onkey: (name) => {
|
|
84
|
-
this.jsonpath.set(name);
|
|
85
|
-
this.currentState.key = name;
|
|
86
|
-
},
|
|
87
|
-
|
|
88
|
-
oncloseobject: () => {
|
|
89
|
-
this._closeObject();
|
|
90
|
-
},
|
|
91
|
-
|
|
92
|
-
onopenarray: () => {
|
|
93
|
-
this._openArray();
|
|
94
|
-
},
|
|
95
|
-
|
|
96
|
-
onclosearray: () => {
|
|
97
|
-
this._closeArray();
|
|
98
|
-
},
|
|
99
|
-
|
|
100
|
-
onvalue: (value) => {
|
|
101
|
-
this._pushOrSet(value);
|
|
102
|
-
},
|
|
103
|
-
|
|
104
|
-
onerror: (error) => {
|
|
105
|
-
throw error;
|
|
106
|
-
},
|
|
107
|
-
|
|
108
|
-
onend: () => {
|
|
109
|
-
this.result = this.currentState.container.pop();
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
protected get parser(): ClarinetParser {
|
|
115
|
-
return this._parser as ClarinetParser;
|
|
116
|
-
}
|
|
117
114
|
}
|