node-opcua-xml2json 2.176.0 → 2.181.0
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/source/definition_parser.d.ts +1 -1
- package/dist/source/definition_parser.js +3 -3
- package/dist/source/definition_parser.js.map +1 -1
- package/dist/source/fragment_cloner.d.ts +5 -5
- package/dist/source/fragment_cloner.js +11 -8
- package/dist/source/fragment_cloner.js.map +1 -1
- package/dist/source/fragment_cloner_parser.d.ts +2 -2
- package/dist/source/fragment_cloner_parser.js +7 -5
- package/dist/source/fragment_cloner_parser.js.map +1 -1
- package/dist/source/index.d.ts +5 -5
- package/dist/source/index.js +5 -5
- package/dist/source/index.js.map +1 -1
- package/dist/source/nodejs/xml2json_fs.d.ts +2 -2
- package/dist/source/nodejs/xml2json_fs.js +4 -4
- package/dist/source/nodejs/xml2json_fs.js.map +1 -1
- package/dist/source/thirdparties/escape.js +9 -14
- package/dist/source/thirdparties/escape.js.map +1 -1
- package/dist/source/thirdparties/parser/lts.d.ts +18 -2
- package/dist/source/thirdparties/parser/lts.js +110 -21
- package/dist/source/thirdparties/parser/lts.js.map +1 -1
- package/dist/source/xml2Json_pojo_tools.d.ts +1 -1
- package/dist/source/xml2Json_pojo_tools.js +2 -2
- package/dist/source/xml2Json_pojo_tools.js.map +1 -1
- package/dist/source/xml2json.d.ts +22 -38
- package/dist/source/xml2json.js +189 -86
- package/dist/source/xml2json.js.map +1 -1
- package/dist/source/xml2json_pojo.d.ts +2 -2
- package/dist/source/xml2json_pojo.js +9 -6
- package/dist/source/xml2json_pojo.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +8 -7
- package/source/definition_parser.ts +9 -5
- package/source/fragment_cloner.ts +8 -7
- package/source/fragment_cloner_parser.ts +8 -6
- package/source/index.ts +5 -5
- package/source/nodejs/xml2json_fs.ts +3 -3
- package/source/thirdparties/escape.ts +9 -20
- package/source/thirdparties/parser/lts.ts +133 -38
- package/source/xml2Json_pojo_tools.ts +3 -5
- package/source/xml2json.ts +219 -98
- package/source/xml2json_pojo.ts +8 -5
package/source/xml2json.ts
CHANGED
|
@@ -3,37 +3,49 @@
|
|
|
3
3
|
* node -> see if https://github.com/isaacs/sax-js could be used instead
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
-
|
|
7
6
|
import { assert } from "node-opcua-assert";
|
|
8
|
-
import { SaxLtx } from "./thirdparties/parser/lts";
|
|
7
|
+
import { SaxLtx } from "./thirdparties/parser/lts.js";
|
|
9
8
|
|
|
10
9
|
export type SimpleCallback = (err?: Error) => void;
|
|
11
10
|
export type Callback<T> = (err?: Error | null, result?: T) => void;
|
|
12
11
|
|
|
13
|
-
|
|
14
12
|
export interface Parser {
|
|
15
13
|
[key: string]: ReaderState;
|
|
16
14
|
}
|
|
17
15
|
|
|
18
16
|
/**
|
|
19
|
-
*
|
|
17
|
+
* the reader states of a definition, in a table of this engine's own. A definition is a plain
|
|
18
|
+
* object the caller may hand to any number of parsers (the shared Definition reader of the
|
|
19
|
+
* nodeset loader, for one), while a reader state carries the element being read: its parent, its
|
|
20
|
+
* attributes, its text so far. So the definition is never written to; each parser builds its
|
|
21
|
+
* states from it. A definition that refers to itself, directly or through a child, gets one state
|
|
22
|
+
* per engine for the cycle, the one being built when the cycle closes.
|
|
20
23
|
* @private
|
|
21
|
-
|
|
22
|
-
* @param parser {map<ReaderState|options>}
|
|
23
|
-
* @return {map}
|
|
24
24
|
*/
|
|
25
|
-
function _coerceParser(parser: ParserLike): Parser {
|
|
25
|
+
function _coerceParser(parser: ParserLike | undefined): Parser {
|
|
26
|
+
// no prototype: an element name is looked up in this table for every element of the document,
|
|
27
|
+
// one hash probe that cannot find an Object member by mistake
|
|
28
|
+
const table: Parser = Object.create(null);
|
|
29
|
+
if (!parser) {
|
|
30
|
+
return table;
|
|
31
|
+
}
|
|
26
32
|
for (const name of Object.keys(parser)) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
delete parser[name];
|
|
31
|
-
parser[name] = new ReaderState(tmp);
|
|
33
|
+
const entry = parser[name];
|
|
34
|
+
if (!entry) {
|
|
35
|
+
continue;
|
|
32
36
|
}
|
|
37
|
+
table[name] = entry instanceof ReaderState ? entry : (statesUnderConstruction.get(entry) ?? new ReaderState(entry));
|
|
33
38
|
}
|
|
34
|
-
return
|
|
39
|
+
return table;
|
|
35
40
|
}
|
|
36
41
|
|
|
42
|
+
/**
|
|
43
|
+
* the states being built from a definition, for the duration of one root construction: the way a
|
|
44
|
+
* definition that closes a cycle finds the state already under way instead of recursing for ever
|
|
45
|
+
*/
|
|
46
|
+
let statesUnderConstruction = new WeakMap<ReaderStateParserLike, ReaderState>();
|
|
47
|
+
let constructionDepth = 0;
|
|
48
|
+
|
|
37
49
|
export interface XmlAttributes {
|
|
38
50
|
[key: string]: string;
|
|
39
51
|
}
|
|
@@ -52,9 +64,16 @@ export interface ParserLike {
|
|
|
52
64
|
|
|
53
65
|
export interface ReaderStateParserLike {
|
|
54
66
|
parser?: ParserLike;
|
|
67
|
+
// `this` here is an open extension point — callers across the workspace attach domain-specific
|
|
68
|
+
// shapes (e.g. EUInformationParserLevel2) that only duck-type against ReaderState, so a concrete
|
|
69
|
+
// `this: ReaderState` breaks those call sites structurally.
|
|
70
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
55
71
|
init?: (this: any, name: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) => void;
|
|
72
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
56
73
|
finish?: (this: any) => void;
|
|
74
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
57
75
|
startElement?: (this: any, name: string, attrs: XmlAttributes) => void;
|
|
76
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
58
77
|
endElement?: (this: any, name: string) => void;
|
|
59
78
|
}
|
|
60
79
|
|
|
@@ -72,8 +91,9 @@ export interface IReaderState {
|
|
|
72
91
|
_on_text(text: string): void;
|
|
73
92
|
}
|
|
74
93
|
|
|
75
|
-
|
|
76
|
-
export
|
|
94
|
+
// biome-ignore lint/suspicious/noUnsafeDeclarationMerging: interface adds typed members/overloads for this class
|
|
95
|
+
export class ReaderStateBase {}
|
|
96
|
+
export interface ReaderStateBase extends IReaderState {}
|
|
77
97
|
/**
|
|
78
98
|
* @private
|
|
79
99
|
*/
|
|
@@ -83,9 +103,9 @@ export class ReaderState extends ReaderStateBase {
|
|
|
83
103
|
public _startElement?: (name: string, attrs: XmlAttributes) => void;
|
|
84
104
|
public _endElement?: (name: string) => void;
|
|
85
105
|
|
|
86
|
-
public parser:
|
|
106
|
+
public parser: Parser;
|
|
87
107
|
public attrs?: XmlAttributes;
|
|
88
|
-
public chunks:
|
|
108
|
+
public chunks: string[] = [];
|
|
89
109
|
public text = "";
|
|
90
110
|
public name? = "";
|
|
91
111
|
public level = -1;
|
|
@@ -95,21 +115,39 @@ export class ReaderState extends ReaderStateBase {
|
|
|
95
115
|
|
|
96
116
|
public parent?: IReaderState;
|
|
97
117
|
public root?: Xml2Json;
|
|
98
|
-
|
|
118
|
+
private _data?: Record<string, unknown>;
|
|
119
|
+
|
|
120
|
+
/** scratch space for the reader, fresh for every element it reads, allocated on first use */
|
|
121
|
+
public get data(): Record<string, unknown> {
|
|
122
|
+
if (!this._data) {
|
|
123
|
+
this._data = {};
|
|
124
|
+
}
|
|
125
|
+
return this._data;
|
|
126
|
+
}
|
|
127
|
+
public set data(value: Record<string, unknown> | undefined) {
|
|
128
|
+
this._data = value;
|
|
129
|
+
}
|
|
99
130
|
|
|
100
131
|
constructor(options: ReaderStateParser | ReaderState) {
|
|
101
132
|
super();
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
133
|
+
if (options instanceof ReaderStateBase) {
|
|
134
|
+
this.parser = options.parser;
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
this._init = options.init;
|
|
138
|
+
this._finish = options.finish;
|
|
139
|
+
this._startElement = options.startElement;
|
|
140
|
+
this._endElement = options.endElement;
|
|
141
|
+
if (constructionDepth === 0) {
|
|
142
|
+
statesUnderConstruction = new WeakMap();
|
|
143
|
+
}
|
|
144
|
+
constructionDepth += 1;
|
|
145
|
+
try {
|
|
146
|
+
statesUnderConstruction.set(options, this);
|
|
147
|
+
this.parser = _coerceParser(options.parser);
|
|
148
|
+
} finally {
|
|
149
|
+
constructionDepth -= 1;
|
|
110
150
|
}
|
|
111
|
-
|
|
112
|
-
this.parser = _coerceParser(options.parser);
|
|
113
151
|
}
|
|
114
152
|
|
|
115
153
|
/**
|
|
@@ -119,7 +157,7 @@ export class ReaderState extends ReaderStateBase {
|
|
|
119
157
|
this.name = elementName;
|
|
120
158
|
this.parent = parent;
|
|
121
159
|
this.engine = engine;
|
|
122
|
-
this.
|
|
160
|
+
this._data = undefined;
|
|
123
161
|
this.level = level;
|
|
124
162
|
this.currentLevel = this.level;
|
|
125
163
|
this.attrs = attrs;
|
|
@@ -149,15 +187,16 @@ export class ReaderState extends ReaderStateBase {
|
|
|
149
187
|
if (this._startElement) {
|
|
150
188
|
this._startElement(elementName, attrs);
|
|
151
189
|
}
|
|
152
|
-
|
|
153
|
-
|
|
190
|
+
const child = this.parser[elementName];
|
|
191
|
+
if (this.engine && child) {
|
|
192
|
+
this.engine._promote(child, level, elementName, attrs);
|
|
154
193
|
}
|
|
155
194
|
}
|
|
156
195
|
|
|
157
196
|
/**
|
|
158
197
|
* @protected
|
|
159
198
|
*/
|
|
160
|
-
public _on_endElement2(
|
|
199
|
+
public _on_endElement2(_level: number, elementName: string): void {
|
|
161
200
|
if (this._endElement) {
|
|
162
201
|
this._endElement(elementName);
|
|
163
202
|
}
|
|
@@ -181,12 +220,8 @@ export class ReaderState extends ReaderStateBase {
|
|
|
181
220
|
this.chunks = [];
|
|
182
221
|
// this is the end
|
|
183
222
|
this._on_finish();
|
|
184
|
-
if (
|
|
185
|
-
this.
|
|
186
|
-
(this.parent as any).parser &&
|
|
187
|
-
Object.prototype.hasOwnProperty.call((this.parent as any).parser, elementName)
|
|
188
|
-
) {
|
|
189
|
-
this.engine!._demote(this, level, elementName);
|
|
223
|
+
if (this.parent instanceof ReaderState && this.parent.parser[elementName]) {
|
|
224
|
+
this.engine?._demote(this, level, elementName);
|
|
190
225
|
}
|
|
191
226
|
}
|
|
192
227
|
}
|
|
@@ -207,15 +242,26 @@ export class ReaderState extends ReaderStateBase {
|
|
|
207
242
|
|
|
208
243
|
const regexp = /(([^:]+):)?(.*)/;
|
|
209
244
|
|
|
210
|
-
|
|
245
|
+
interface ResolvedName {
|
|
246
|
+
ns: string | undefined;
|
|
247
|
+
tag: string;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
// a document uses a few dozen distinct element names, each of them thousands of times
|
|
251
|
+
const resolvedNames = new Map<string, ResolvedName>();
|
|
252
|
+
|
|
253
|
+
function resolve_namespace(name: string): ResolvedName {
|
|
254
|
+
const known = resolvedNames.get(name);
|
|
255
|
+
if (known) {
|
|
256
|
+
return known;
|
|
257
|
+
}
|
|
211
258
|
const m = name.match(regexp);
|
|
212
259
|
if (!m) {
|
|
213
260
|
throw new Error("Invalid match");
|
|
214
261
|
}
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
};
|
|
262
|
+
const resolved: ResolvedName = { ns: m[2], tag: m[3] };
|
|
263
|
+
resolvedNames.set(name, resolved);
|
|
264
|
+
return resolved;
|
|
219
265
|
}
|
|
220
266
|
|
|
221
267
|
/**
|
|
@@ -251,17 +297,29 @@ function resolve_namespace(name: string) {
|
|
|
251
297
|
* done();
|
|
252
298
|
* });
|
|
253
299
|
*/
|
|
300
|
+
/** how many times a reader state is active at once, kept on the state itself */
|
|
301
|
+
const activations = Symbol("activations");
|
|
302
|
+
interface ActivationCounted {
|
|
303
|
+
[activations]?: number;
|
|
304
|
+
}
|
|
305
|
+
|
|
254
306
|
export class Xml2Json {
|
|
255
307
|
public currentLevel = 0;
|
|
256
|
-
|
|
308
|
+
// two parallel stacks rather than one record per element: an element is pushed and popped
|
|
309
|
+
// for each of the hundreds of thousands a nodeset carries
|
|
310
|
+
private state_stack: Array<IReaderState | null> = [];
|
|
311
|
+
private backup_stack: Array<Record<string, unknown> | null> = [];
|
|
257
312
|
private current_state: IReaderState | null = null;
|
|
258
|
-
private
|
|
313
|
+
private sax: SaxLtx | null = null;
|
|
314
|
+
private decoder: TextDecoder | null = null;
|
|
315
|
+
private atStart = false;
|
|
259
316
|
|
|
260
317
|
constructor(options: ReaderStateParser) {
|
|
261
318
|
const state = options instanceof ReaderStateBase ? (options as ReaderState) : new ReaderState(options);
|
|
262
319
|
state.root = this;
|
|
263
320
|
|
|
264
321
|
this.state_stack = [];
|
|
322
|
+
this.backup_stack = [];
|
|
265
323
|
this.current_state = null;
|
|
266
324
|
this._promote(state, 0);
|
|
267
325
|
}
|
|
@@ -269,6 +327,102 @@ export class Xml2Json {
|
|
|
269
327
|
public parseString(xml_text: string): Record<string, unknown> {
|
|
270
328
|
return this.__parseInternal(xml_text);
|
|
271
329
|
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* parse a document that arrives in pieces: text, or UTF-8 bytes decoded as they come (a
|
|
333
|
+
* multi-byte character or the byte-order mark may straddle two chunks)
|
|
334
|
+
*/
|
|
335
|
+
public async parseStream(
|
|
336
|
+
chunks: AsyncIterable<string | Uint8Array> | Iterable<string | Uint8Array>
|
|
337
|
+
): Promise<Record<string, unknown>> {
|
|
338
|
+
this.begin();
|
|
339
|
+
for await (const chunk of chunks) {
|
|
340
|
+
this.write(chunk);
|
|
341
|
+
}
|
|
342
|
+
return this.end();
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/** start a document that {@link write} will feed and {@link end} will finish */
|
|
346
|
+
public begin(): void {
|
|
347
|
+
this.currentLevel = 0;
|
|
348
|
+
this.atStart = true;
|
|
349
|
+
this.decoder = null;
|
|
350
|
+
// direct handlers rather than events, and the blank runs between tags dropped by the
|
|
351
|
+
// parser before they are unescaped: see SaxLtxHandlers
|
|
352
|
+
const sax = new SaxLtx({
|
|
353
|
+
skipBlankText: true,
|
|
354
|
+
onStartElement: (name: string, attrs: XmlAttributes) => {
|
|
355
|
+
const tag_ns = resolve_namespace(name);
|
|
356
|
+
this.currentLevel += 1;
|
|
357
|
+
if (this.current_state) {
|
|
358
|
+
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
359
|
+
}
|
|
360
|
+
},
|
|
361
|
+
onEndElement: (name: string) => {
|
|
362
|
+
const tag_ns = resolve_namespace(name);
|
|
363
|
+
if (this.current_state) {
|
|
364
|
+
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
365
|
+
}
|
|
366
|
+
this.currentLevel -= 1;
|
|
367
|
+
if (this.currentLevel === 0) {
|
|
368
|
+
sax.emit("close");
|
|
369
|
+
}
|
|
370
|
+
},
|
|
371
|
+
onText: (text: string) => {
|
|
372
|
+
// the reader state trims what it receives; nothing blank reaches this point
|
|
373
|
+
if (this.current_state) {
|
|
374
|
+
this.current_state._on_text(text);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
});
|
|
378
|
+
this.sax = sax;
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/** feed the next piece of the document started by {@link begin} */
|
|
382
|
+
public write(chunk: string | Uint8Array): void {
|
|
383
|
+
const sax = this.sax;
|
|
384
|
+
if (!sax) {
|
|
385
|
+
throw new Error("Xml2Json#write: begin() must be called first");
|
|
386
|
+
}
|
|
387
|
+
let text: string;
|
|
388
|
+
if (typeof chunk === "string") {
|
|
389
|
+
text = chunk;
|
|
390
|
+
} else {
|
|
391
|
+
this.decoder = this.decoder || new TextDecoder("utf-8");
|
|
392
|
+
text = this.decoder.decode(chunk, { stream: true });
|
|
393
|
+
}
|
|
394
|
+
if (text.length === 0) {
|
|
395
|
+
return;
|
|
396
|
+
}
|
|
397
|
+
if (this.atStart) {
|
|
398
|
+
this.atStart = false;
|
|
399
|
+
if (text.charCodeAt(0) === 0xfeff) {
|
|
400
|
+
text = text.slice(1);
|
|
401
|
+
}
|
|
402
|
+
}
|
|
403
|
+
sax.write(text);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/** finish the document started by {@link begin} and return what the reader states built */
|
|
407
|
+
public end(): Record<string, unknown> {
|
|
408
|
+
const sax = this.sax;
|
|
409
|
+
if (!sax) {
|
|
410
|
+
throw new Error("Xml2Json#end: begin() must be called first");
|
|
411
|
+
}
|
|
412
|
+
if (this.decoder) {
|
|
413
|
+
const tail = this.decoder.decode();
|
|
414
|
+
this.decoder = null;
|
|
415
|
+
if (tail.length > 0) {
|
|
416
|
+
sax.write(tail);
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
sax.end("");
|
|
420
|
+
this.sax = null;
|
|
421
|
+
if (!this.current_state) {
|
|
422
|
+
return {};
|
|
423
|
+
}
|
|
424
|
+
return (this.current_state as unknown as { _pojo: Record<string, unknown> })._pojo;
|
|
425
|
+
}
|
|
272
426
|
/**
|
|
273
427
|
* @private
|
|
274
428
|
* @internal
|
|
@@ -280,16 +434,19 @@ export class Xml2Json {
|
|
|
280
434
|
// active at two levels at once. This happens when a data type refers to itself, directly or
|
|
281
435
|
// indirectly: the reader of the nested element is then the very same object as the outer one.
|
|
282
436
|
// In that case only, take a copy of the outer activation and restore it in _demote.
|
|
283
|
-
const
|
|
284
|
-
|
|
437
|
+
const counted = new_state as ActivationCounted;
|
|
438
|
+
const active = counted[activations] || 0;
|
|
439
|
+
counted[activations] = active + 1;
|
|
285
440
|
|
|
286
|
-
this.state_stack.push(
|
|
287
|
-
|
|
288
|
-
state: this.current_state
|
|
289
|
-
});
|
|
441
|
+
this.state_stack.push(this.current_state);
|
|
442
|
+
this.backup_stack.push(active > 0 ? { ...(new_state as unknown as Record<string, unknown>) } : null);
|
|
290
443
|
|
|
291
444
|
const parent = this.current_state;
|
|
292
445
|
this.current_state = new_state;
|
|
446
|
+
// parent is null only for the root element; every _on_init implementation in this codebase
|
|
447
|
+
// names the parameter `_parent` (unused), so widening the public IReaderState signature to
|
|
448
|
+
// accept null would ripple into every consumer package that already treats it as non-null.
|
|
449
|
+
// biome-ignore lint/style/noNonNullAssertion: see comment above
|
|
293
450
|
this.current_state._on_init(name || "???", attr, parent!, level, this);
|
|
294
451
|
}
|
|
295
452
|
|
|
@@ -299,18 +456,18 @@ export class Xml2Json {
|
|
|
299
456
|
*/
|
|
300
457
|
public _demote(cur_state: IReaderState, level: number, elementName: string): void {
|
|
301
458
|
/// assert(this.current_state === cur_state);
|
|
302
|
-
|
|
459
|
+
if (this.state_stack.length === 0) {
|
|
460
|
+
throw new Error("_demote called without a matching _promote");
|
|
461
|
+
}
|
|
462
|
+
const state = this.state_stack.pop() as IReaderState | null;
|
|
463
|
+
const backup = this.backup_stack.pop() as Record<string, unknown> | null;
|
|
303
464
|
this.current_state = state;
|
|
304
465
|
if (this.current_state) {
|
|
305
466
|
this.current_state._on_endElement2(level, elementName);
|
|
306
467
|
}
|
|
307
468
|
|
|
308
|
-
const
|
|
309
|
-
|
|
310
|
-
this.activation_count.delete(cur_state);
|
|
311
|
-
} else {
|
|
312
|
-
this.activation_count.set(cur_state, count);
|
|
313
|
-
}
|
|
469
|
+
const counted = cur_state as ActivationCounted;
|
|
470
|
+
counted[activations] = (counted[activations] || 1) - 1;
|
|
314
471
|
if (backup) {
|
|
315
472
|
// cur_state was a nested activation of a state that is still active at an outer level:
|
|
316
473
|
// put the outer element back the way it was. The parent has already consumed the nested
|
|
@@ -328,44 +485,8 @@ export class Xml2Json {
|
|
|
328
485
|
* @internal
|
|
329
486
|
*/
|
|
330
487
|
protected __parseInternal(data: string): Record<string, unknown> {
|
|
331
|
-
|
|
332
|
-
this.
|
|
333
|
-
|
|
334
|
-
const tag_ns = resolve_namespace(name);
|
|
335
|
-
this.currentLevel += 1;
|
|
336
|
-
if (this.current_state) {
|
|
337
|
-
this.current_state._on_startElement(this.currentLevel, tag_ns.tag, attrs);
|
|
338
|
-
}
|
|
339
|
-
});
|
|
340
|
-
parser.on("endElement", (name: string) => {
|
|
341
|
-
const tag_ns = resolve_namespace(name);
|
|
342
|
-
if (this.current_state) {
|
|
343
|
-
this.current_state._on_endElement(this.currentLevel, tag_ns.tag);
|
|
344
|
-
}
|
|
345
|
-
this.currentLevel -= 1;
|
|
346
|
-
if (this.currentLevel === 0) {
|
|
347
|
-
parser.emit("close");
|
|
348
|
-
}
|
|
349
|
-
});
|
|
350
|
-
parser.on("text", (text: string) => {
|
|
351
|
-
text = text.trim();
|
|
352
|
-
if (text.length === 0) {
|
|
353
|
-
return;
|
|
354
|
-
}
|
|
355
|
-
if (this.current_state) {
|
|
356
|
-
this.current_state._on_text(text);
|
|
357
|
-
}
|
|
358
|
-
});
|
|
359
|
-
parser.write(data);
|
|
360
|
-
parser.end("");
|
|
361
|
-
return (this.current_state! as any)._pojo;
|
|
362
|
-
/*
|
|
363
|
-
return await new Promise((resolve) => {
|
|
364
|
-
parser.once("close", () => {
|
|
365
|
-
resolve((this.current_state! as any)._pojo);
|
|
366
|
-
});
|
|
367
|
-
//parser.write(data);
|
|
368
|
-
parser.end(data);
|
|
369
|
-
})*/
|
|
488
|
+
this.begin();
|
|
489
|
+
this.write(data);
|
|
490
|
+
return this.end();
|
|
370
491
|
}
|
|
371
492
|
}
|
package/source/xml2json_pojo.ts
CHANGED
|
@@ -1,21 +1,24 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import { ReaderState2, type withPojoLambda } from "./xml2Json_pojo_tools.js";
|
|
2
|
+
import { type IReaderState, type ReaderState, type ReaderStateParser, Xml2Json, type XmlAttributes } from "./xml2json.js";
|
|
3
3
|
|
|
4
4
|
const json_extractor: ReaderState2 = new ReaderState2();
|
|
5
5
|
export const json_parser: ReaderStateParser = {
|
|
6
6
|
init(this: IReaderState, elementName: string, attrs: XmlAttributes, parent: IReaderState, engine: Xml2Json) {
|
|
7
7
|
json_extractor._on_init(elementName, attrs, parent, 0, engine);
|
|
8
8
|
},
|
|
9
|
+
// ReaderStateParser.finish declares `this: IReaderState`, which doesn't expose `.parent` — the
|
|
10
|
+
// runtime `this` here is always the ReaderState instance that owns this parser entry.
|
|
11
|
+
// biome-ignore lint/suspicious/noExplicitAny: see comment above
|
|
9
12
|
finish(this: any) {
|
|
10
13
|
this.parent._pojo = json_extractor._pojo;
|
|
11
14
|
}
|
|
12
15
|
};
|
|
13
16
|
|
|
14
17
|
export function startPojo(pThis: ReaderState, elementName: string, attrs: XmlAttributes, withPojo: withPojoLambda): void {
|
|
15
|
-
pThis.engine
|
|
16
|
-
json_extractor._withPojo = (name: string, pojo:
|
|
18
|
+
pThis.engine?._promote(json_extractor, pThis.engine?.currentLevel, elementName, attrs);
|
|
19
|
+
json_extractor._withPojo = (name: string, pojo: unknown) => {
|
|
17
20
|
withPojo(name, pojo);
|
|
18
|
-
pThis.engine
|
|
21
|
+
pThis.engine?._demote(json_extractor, pThis.engine?.currentLevel, elementName);
|
|
19
22
|
};
|
|
20
23
|
}
|
|
21
24
|
|