Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add XML to JSON operation #1631

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,7 @@
"ssdeep.js": "0.0.3",
"stream-browserify": "^3.0.0",
"tesseract.js": "3.0.3",
"timers": "^0.1.1",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

"ua-parser-js": "^1.0.34",
"unorm": "^1.6.0",
"utf8": "^3.0.0",
Expand Down
1 change: 1 addition & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
"CSV to JSON",
"JSON to CSV",
"Avro to JSON",
"XML to JSON",
"CBOR Encode",
"CBOR Decode"
]
Expand Down
49 changes: 49 additions & 0 deletions src/core/operations/XMLToJSON.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/**
* @author jpledref [jp.ledref@gmail.com]
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/

import xml2js from "xml2js";
import Operation from "../Operation.mjs";
import OperationError from "../errors/OperationError.mjs";

/**
* XML to JSON operation
*/
class XMLToJSON extends Operation {

/**
* XMLToJSON constructor
*/
constructor() {
super();

this.name = "XML to JSON";
this.module = "Default";
this.description = "Converts XML data to JSON format.";
this.infoURL = "https://wikipedia.org/wiki/XML";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
];
}

/**
* @param {string} input
* @param {Object[]} args
* @returns {JSON}
*/
run(input, args) {
let result;
try {
xml2js.parseString(input, {}, (e, r) => result = JSON.stringify(r));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're not using the error result in the callback. I assume xml2js uses that rather than throwing an error, and it's going to include useful parser errors.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're taking the return value, encoding it into JSON and then parsing it back again. That seems unnecessary,

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a callback here: is xml2js synchronous or asynchronous? It might be safer to return a Promise that handles the callback result.

return JSON.parse(result);
} catch (err) {
throw new OperationError("Unable to parse XML to JSON: " + err.toString());
}
}

}

export default XMLToJSON;
24 changes: 24 additions & 0 deletions tests/operations/tests/XMLToJSON.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* XML to JSON tests.
*
* @author jpledref [jp.ledref@gmail.com]
* @copyright Crown Copyright 2023
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

const EXPECTED_JSON = { "root": {"shelf": ["over"], "wood": ["do"], "natural": ["sale"]}};

TestRegister.addTests([
{
name: "XML to JSON",
input: "<root>\n<shelf>over</shelf>\n<wood>do</wood>\n<natural>sale</natural>\n</root>",
expectedOutput: EXPECTED_JSON,
recipeConfig: [
{
op: "XML to JSON",
args: []
},
],
}
]);