-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -66,6 +66,7 @@ | |
"CSV to JSON", | ||
"JSON to CSV", | ||
"Avro to JSON", | ||
"XML to JSON", | ||
"CBOR Encode", | ||
"CBOR Decode" | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/** | ||
* @author jpledref [[email protected]] | ||
* @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)); | ||
return JSON.parse(result); | ||
} catch (err) { | ||
throw new OperationError("Unable to parse XML to JSON: " + err.toString()); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default XMLToJSON; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* XML to JSON tests. | ||
* | ||
* @author jpledref [[email protected]] | ||
* @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: [] | ||
}, | ||
], | ||
} | ||
]); |