Data Transformations Package
Table of Contents
Overview
The Data Transformations package contains helper utilities to work with JSON,
XML and CSV data formats.
XML
Async streaming XML Parser and Writer.
XmlParser
receives strings with parts of the XML content in the
"write" method and the callback given to parser constructor receives events
of type XmlEvent.
XmlWriter
is the opposite component. It receives XmlEvent
objects in the
write
method and the callback given to constructor receives a string with the
XML content.
There are also predefined TransformStream classes StringToXmlTransformStream
and
XmlToStringTransformStream
for convenient use with streams.
XML Example
let writer = new XmlWriter(async (str) => {
console.log(str);
});
let parser = new XmlParser(async (event) => {
if (event.type === XmlEventType.START_TAG) {
let elem = event.details as ElementDetails;
if (elem.name == "name") {
elem.name = "fixedName"
}
} else if (event.type === XmlEventType.END_TAG) {
if (event.details === "name") {
event.details = "fixedName"
}
}
await writer.write(event);
});
await parser.parse(`<person><name>Fred</name></person>`);
await parser.flush();
await writer.flush();
Output:
<person><fixedName>Fred</fixedName></person>
JSON
Async streaming JSON Parser and Writer.
JsonParser
receives strings with parts of the JSON content in the
write
method and the callback given to parser constructor receives events
of type JsonEvent.
JsonWriter
is the opposite component. It receives JsonEvent objects in the
write
method and the callback given to constructor receives a string with the
JSON content.
JSON Example
let writer = new JsonWriter(async (str) => {
console.log(str);
});
let parser = new JsonParser(async (event) => {
if(event.type === JsonEventType.PROPERTY_NAME && event.data === "name") {
event.data = "fixedName";
}
await writer.write(event);
})
await parser.parse(`{"person": {"name":"Fred"}}`);
await parser.flush();
await writer.flush();
Output:
{"person":{"fixedName":"Fred"}}