From 23763363bab5c24338c89f7131ebed7921dc5951 Mon Sep 17 00:00:00 2001 From: Philipp Arnold Date: Fri, 10 Dec 2021 23:22:57 +0100 Subject: [PATCH 1/8] Added a JSON to YAML and a YALM to JSON operation --- src/core/config/Categories.json | 4 ++- src/core/operations/JSONToYAML.mjs | 46 ++++++++++++++++++++++++++++++ src/core/operations/YAMLToJSON.mjs | 45 +++++++++++++++++++++++++++++ tests/operations/index.mjs | 1 + tests/operations/tests/YAML.mjs | 42 +++++++++++++++++++++++++++ 5 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 src/core/operations/JSONToYAML.mjs create mode 100644 src/core/operations/YAMLToJSON.mjs create mode 100644 tests/operations/tests/YAML.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 09ee8d15..e35bc851 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -63,7 +63,9 @@ "JSON to CSV", "Avro to JSON", "CBOR Encode", - "CBOR Decode" + "CBOR Decode", + "YAML to JSON", + "JSON to YAML" ] }, { diff --git a/src/core/operations/JSONToYAML.mjs b/src/core/operations/JSONToYAML.mjs new file mode 100644 index 00000000..2c8102e0 --- /dev/null +++ b/src/core/operations/JSONToYAML.mjs @@ -0,0 +1,46 @@ +/** + * @author ccarpo [ccarpo@gmx.net] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import YAML from "yaml"; + +/** + * JSON to YAML operation + */ +class JSONToYAML extends Operation { + + /** + * JSONToYAML constructor + */ + constructor() { + super(); + + this.name = "JSON to YAML"; + this.module = "Default"; + this.description = "Converts a JSON into a YAML"; + this.infoURL = "https://en.wikipedia.org/wiki/YAML"; + this.inputType = "JSON"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {JSON} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + try { + return YAML.stringify(input); + } catch (err) { + throw new OperationError("Test"); + } + } + +} + +export default JSONToYAML; diff --git a/src/core/operations/YAMLToJSON.mjs b/src/core/operations/YAMLToJSON.mjs new file mode 100644 index 00000000..4f77f4fd --- /dev/null +++ b/src/core/operations/YAMLToJSON.mjs @@ -0,0 +1,45 @@ +/** + * @author ccarpo [ccarpo@gmx.net] + * @copyright Crown Copyright 2021 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; +import jsYaml from "js-yaml"; +/** + * YAML to JSON operation + */ +class YAMLToJSON extends Operation { + + /** + * YAMLToJSON constructor + */ + constructor() { + super(); + + this.name = "YAML to JSON"; + this.module = "Default"; + this.description = "Converts a YAML to JSON"; + this.infoURL = "https://en.wikipedia.org/wiki/YAML"; + this.inputType = "string"; + this.outputType = "JSON"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {JSON} + */ + run(input, args) { + try { + return jsYaml.load(input); + } catch (err) { + throw new OperationError("Unable to parse YAML: " + err); + } + } + +} + +export default YAMLToJSON; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 9add20b9..3f7a7457 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -107,6 +107,7 @@ import "./tests/CBORDecode.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3SFingerprint.mjs"; import "./tests/HASSH.mjs"; +import "./tests/YAML.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/YAML.mjs b/tests/operations/tests/YAML.mjs new file mode 100644 index 00000000..ba72ed43 --- /dev/null +++ b/tests/operations/tests/YAML.mjs @@ -0,0 +1,42 @@ + +/** + * YAML tests. + * + * @author ccarpo [ccarpo@gmx.net] + * + * @copyright Crown Copyright 2018 + * @license Apache-2.0 + */ +import TestRegister from "../../lib/TestRegister.mjs"; + +const EXAMPLE_YAML = `number: 3\nplain: string\nblock: |\n two\n lines`; +const EXAMPLE_JSON = `{ "number": 3, "plain": "string" }`; + +TestRegister.addTests([ + { + name: "YAML to JSON", + input: EXAMPLE_YAML, + expectedOutput: JSON.stringify({ + "number": 3, + "plain": "string", + "block": "two\nlines\n" + }, null, 4), + recipeConfig: [ + { + op: "YAML to JSON", + args: [], + } + ], + }, + { + name: "JSON to YAML", + input: EXAMPLE_JSON, + expectedOutput: `number: 3\nplain: string\n`, + recipeConfig: [ + { + op: "JSON to YAML", + args: [], + } + ], + }, +]); From 7353315baaedb2113cda178d03a88cafc7f02a4e Mon Sep 17 00:00:00 2001 From: ccarpo Date: Tue, 21 Nov 2023 08:56:20 +0100 Subject: [PATCH 2/8] Renamed JSON to Yaml to Beautify JSON --- src/core/config/Categories.json | 2 +- .../operations/{JSONToYAML.mjs => BeautifyYAML.mjs} | 12 ++++++------ src/core/operations/YAMLToJSON.mjs | 2 +- tests/operations/index.mjs | 2 +- .../operations/tests/{YAML.mjs => BeautifyYAML.mjs} | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) rename src/core/operations/{JSONToYAML.mjs => BeautifyYAML.mjs} (76%) rename tests/operations/tests/{YAML.mjs => BeautifyYAML.mjs} (89%) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index e35bc851..ce1a639b 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -65,7 +65,7 @@ "CBOR Encode", "CBOR Decode", "YAML to JSON", - "JSON to YAML" + "Beatufiy YAML" ] }, { diff --git a/src/core/operations/JSONToYAML.mjs b/src/core/operations/BeautifyYAML.mjs similarity index 76% rename from src/core/operations/JSONToYAML.mjs rename to src/core/operations/BeautifyYAML.mjs index 2c8102e0..b87573d8 100644 --- a/src/core/operations/JSONToYAML.mjs +++ b/src/core/operations/BeautifyYAML.mjs @@ -9,19 +9,19 @@ import OperationError from "../errors/OperationError.mjs"; import YAML from "yaml"; /** - * JSON to YAML operation + * Beautify YAML operation */ -class JSONToYAML extends Operation { +class BeautifyYAML extends Operation { /** - * JSONToYAML constructor + * BeautifyYAML constructor */ constructor() { super(); - this.name = "JSON to YAML"; + this.name = "Beautify YAML"; this.module = "Default"; - this.description = "Converts a JSON into a YAML"; + this.description = "Format a JSON object into YAML"; this.infoURL = "https://en.wikipedia.org/wiki/YAML"; this.inputType = "JSON"; this.outputType = "string"; @@ -43,4 +43,4 @@ class JSONToYAML extends Operation { } -export default JSONToYAML; +export default BeautifyYAML; diff --git a/src/core/operations/YAMLToJSON.mjs b/src/core/operations/YAMLToJSON.mjs index 4f77f4fd..5b986575 100644 --- a/src/core/operations/YAMLToJSON.mjs +++ b/src/core/operations/YAMLToJSON.mjs @@ -20,7 +20,7 @@ class YAMLToJSON extends Operation { this.name = "YAML to JSON"; this.module = "Default"; - this.description = "Converts a YAML to JSON"; + this.description = "Convert YAML to JSON"; this.infoURL = "https://en.wikipedia.org/wiki/YAML"; this.inputType = "string"; this.outputType = "JSON"; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 3f7a7457..475d6d31 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -107,7 +107,7 @@ import "./tests/CBORDecode.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3SFingerprint.mjs"; import "./tests/HASSH.mjs"; -import "./tests/YAML.mjs"; +import "./tests/BeautifyYAML.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/YAML.mjs b/tests/operations/tests/BeautifyYAML.mjs similarity index 89% rename from tests/operations/tests/YAML.mjs rename to tests/operations/tests/BeautifyYAML.mjs index ba72ed43..f2cc58fb 100644 --- a/tests/operations/tests/YAML.mjs +++ b/tests/operations/tests/BeautifyYAML.mjs @@ -4,7 +4,7 @@ * * @author ccarpo [ccarpo@gmx.net] * - * @copyright Crown Copyright 2018 + * @copyright Crown Copyright 2021 * @license Apache-2.0 */ import TestRegister from "../../lib/TestRegister.mjs"; @@ -29,12 +29,12 @@ TestRegister.addTests([ ], }, { - name: "JSON to YAML", + name: "Beautify YAML", input: EXAMPLE_JSON, expectedOutput: `number: 3\nplain: string\n`, recipeConfig: [ { - op: "JSON to YAML", + op: "Beautify YAML", args: [], } ], From 4255d8d543226969381adc1c6a06f421e76ac6d8 Mon Sep 17 00:00:00 2001 From: ccarpo Date: Tue, 21 Nov 2023 09:19:58 +0100 Subject: [PATCH 3/8] Renamed JSON to Yaml to Beautify JSON --- src/core/config/Categories.json | 2 +- .../operations/{JSONToYAML.mjs => BeautifyYAML.mjs} | 12 ++++++------ src/core/operations/YAMLToJSON.mjs | 2 +- tests/operations/index.mjs | 2 +- .../operations/tests/{YAML.mjs => BeautifyYAML.mjs} | 6 +++--- 5 files changed, 12 insertions(+), 12 deletions(-) rename src/core/operations/{JSONToYAML.mjs => BeautifyYAML.mjs} (76%) rename tests/operations/tests/{YAML.mjs => BeautifyYAML.mjs} (89%) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index e35bc851..6e5e7f04 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -65,7 +65,7 @@ "CBOR Encode", "CBOR Decode", "YAML to JSON", - "JSON to YAML" + "Beautify YAML" ] }, { diff --git a/src/core/operations/JSONToYAML.mjs b/src/core/operations/BeautifyYAML.mjs similarity index 76% rename from src/core/operations/JSONToYAML.mjs rename to src/core/operations/BeautifyYAML.mjs index 2c8102e0..b87573d8 100644 --- a/src/core/operations/JSONToYAML.mjs +++ b/src/core/operations/BeautifyYAML.mjs @@ -9,19 +9,19 @@ import OperationError from "../errors/OperationError.mjs"; import YAML from "yaml"; /** - * JSON to YAML operation + * Beautify YAML operation */ -class JSONToYAML extends Operation { +class BeautifyYAML extends Operation { /** - * JSONToYAML constructor + * BeautifyYAML constructor */ constructor() { super(); - this.name = "JSON to YAML"; + this.name = "Beautify YAML"; this.module = "Default"; - this.description = "Converts a JSON into a YAML"; + this.description = "Format a JSON object into YAML"; this.infoURL = "https://en.wikipedia.org/wiki/YAML"; this.inputType = "JSON"; this.outputType = "string"; @@ -43,4 +43,4 @@ class JSONToYAML extends Operation { } -export default JSONToYAML; +export default BeautifyYAML; diff --git a/src/core/operations/YAMLToJSON.mjs b/src/core/operations/YAMLToJSON.mjs index 4f77f4fd..5b986575 100644 --- a/src/core/operations/YAMLToJSON.mjs +++ b/src/core/operations/YAMLToJSON.mjs @@ -20,7 +20,7 @@ class YAMLToJSON extends Operation { this.name = "YAML to JSON"; this.module = "Default"; - this.description = "Converts a YAML to JSON"; + this.description = "Convert YAML to JSON"; this.infoURL = "https://en.wikipedia.org/wiki/YAML"; this.inputType = "string"; this.outputType = "JSON"; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 3f7a7457..475d6d31 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -107,7 +107,7 @@ import "./tests/CBORDecode.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3SFingerprint.mjs"; import "./tests/HASSH.mjs"; -import "./tests/YAML.mjs"; +import "./tests/BeautifyYAML.mjs"; // Cannot test operations that use the File type yet diff --git a/tests/operations/tests/YAML.mjs b/tests/operations/tests/BeautifyYAML.mjs similarity index 89% rename from tests/operations/tests/YAML.mjs rename to tests/operations/tests/BeautifyYAML.mjs index ba72ed43..f2cc58fb 100644 --- a/tests/operations/tests/YAML.mjs +++ b/tests/operations/tests/BeautifyYAML.mjs @@ -4,7 +4,7 @@ * * @author ccarpo [ccarpo@gmx.net] * - * @copyright Crown Copyright 2018 + * @copyright Crown Copyright 2021 * @license Apache-2.0 */ import TestRegister from "../../lib/TestRegister.mjs"; @@ -29,12 +29,12 @@ TestRegister.addTests([ ], }, { - name: "JSON to YAML", + name: "Beautify YAML", input: EXAMPLE_JSON, expectedOutput: `number: 3\nplain: string\n`, recipeConfig: [ { - op: "JSON to YAML", + op: "Beautify YAML", args: [], } ], From cc21fe18ca450e05a8e71c1d1c0b58c460127d94 Mon Sep 17 00:00:00 2001 From: ccarpo Date: Tue, 21 Nov 2023 09:24:54 +0100 Subject: [PATCH 4/8] fixed typo. --- src/core/config/Categories.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index ce1a639b..6e5e7f04 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -65,7 +65,7 @@ "CBOR Encode", "CBOR Decode", "YAML to JSON", - "Beatufiy YAML" + "Beautify YAML" ] }, { From 06b7f0129ff684786f39fbca6e298a7e2271ec53 Mon Sep 17 00:00:00 2001 From: CCarpo Date: Mon, 17 Feb 2025 08:42:14 +0100 Subject: [PATCH 5/8] fixed linter issues. --- tests/operations/tests/BeautifyYAML.mjs | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/tests/operations/tests/BeautifyYAML.mjs b/tests/operations/tests/BeautifyYAML.mjs index f2cc58fb..348991d8 100644 --- a/tests/operations/tests/BeautifyYAML.mjs +++ b/tests/operations/tests/BeautifyYAML.mjs @@ -1,4 +1,3 @@ - /** * YAML tests. * @@ -17,10 +16,10 @@ TestRegister.addTests([ name: "YAML to JSON", input: EXAMPLE_YAML, expectedOutput: JSON.stringify({ - "number": 3, - "plain": "string", - "block": "two\nlines\n" - }, null, 4), + "number": 3, + "plain": "string", + "block": "two\nlines\n" + }, null, 4), recipeConfig: [ { op: "YAML to JSON", From c2936a6f2c5eeece72b62271ccef976fddbce42b Mon Sep 17 00:00:00 2001 From: CCarpo Date: Mon, 17 Feb 2025 08:44:42 +0100 Subject: [PATCH 6/8] removed old data format --- src/core/config/Categories.json | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 234ae6cc..4b713f40 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -73,8 +73,7 @@ "CBOR Encode", "CBOR Decode", "YAML to JSON", - "Beautify YAML" - "Caret/M-decode", + "Beautify YAML", "Rison Encode", "Rison Decode", "To Modhex", From 5cef2b13a3446d4e22a896a9ab491797d384e024 Mon Sep 17 00:00:00 2001 From: CCarpo Date: Mon, 17 Feb 2025 08:46:28 +0100 Subject: [PATCH 7/8] fixed typo in categories. --- src/core/config/Categories.json | 1 + 1 file changed, 1 insertion(+) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 4b713f40..1ca519a0 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -74,6 +74,7 @@ "CBOR Decode", "YAML to JSON", "Beautify YAML", + "Caret/M-decode", "Rison Encode", "Rison Decode", "To Modhex", From a809321b639d2e16c7cbfbbf2b683e0c2d04d8d4 Mon Sep 17 00:00:00 2001 From: CCarpo Date: Mon, 10 Mar 2025 20:55:35 +0100 Subject: [PATCH 8/8] renamed function for clarity --- src/core/config/Categories.json | 2 +- .../operations/{BeautifyYAML.mjs => JSONtoYAML.mjs} | 10 +++++----- tests/operations/index.mjs | 4 +--- .../tests/{BeautifyYAML.mjs => JSONtoYAML.mjs} | 4 ++-- 4 files changed, 9 insertions(+), 11 deletions(-) rename src/core/operations/{BeautifyYAML.mjs => JSONtoYAML.mjs} (83%) rename tests/operations/tests/{BeautifyYAML.mjs => JSONtoYAML.mjs} (92%) diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 1ca519a0..2833cb0a 100644 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -73,7 +73,7 @@ "CBOR Encode", "CBOR Decode", "YAML to JSON", - "Beautify YAML", + "JSON to YAML", "Caret/M-decode", "Rison Encode", "Rison Decode", diff --git a/src/core/operations/BeautifyYAML.mjs b/src/core/operations/JSONtoYAML.mjs similarity index 83% rename from src/core/operations/BeautifyYAML.mjs rename to src/core/operations/JSONtoYAML.mjs index b87573d8..0d4cc626 100644 --- a/src/core/operations/BeautifyYAML.mjs +++ b/src/core/operations/JSONtoYAML.mjs @@ -9,17 +9,17 @@ import OperationError from "../errors/OperationError.mjs"; import YAML from "yaml"; /** - * Beautify YAML operation + * JSON to YAML operation */ -class BeautifyYAML extends Operation { +class JSONtoYAML extends Operation { /** - * BeautifyYAML constructor + * JSONtoYAML constructor */ constructor() { super(); - this.name = "Beautify YAML"; + this.name = "JSON to YAML"; this.module = "Default"; this.description = "Format a JSON object into YAML"; this.infoURL = "https://en.wikipedia.org/wiki/YAML"; @@ -43,4 +43,4 @@ class BeautifyYAML extends Operation { } -export default BeautifyYAML; +export default JSONtoYAML; diff --git a/tests/operations/index.mjs b/tests/operations/index.mjs index 713496c7..1c25a2ab 100644 --- a/tests/operations/index.mjs +++ b/tests/operations/index.mjs @@ -166,12 +166,10 @@ import "./tests/CBORDecode.mjs"; import "./tests/JA3Fingerprint.mjs"; import "./tests/JA3SFingerprint.mjs"; import "./tests/HASSH.mjs"; -import "./tests/BeautifyYAML.mjs"; - +import "./tests/JSONtoYAML.mjs"; // Cannot test operations that use the File type yet // import "./tests/SplitColourChannels.mjs"; -import "./tests/BeautifyYAML.mjs"; import "./tests/YARA.mjs"; import "./tests/ParseCSR.mjs"; import "./tests/XXTEA.mjs"; diff --git a/tests/operations/tests/BeautifyYAML.mjs b/tests/operations/tests/JSONtoYAML.mjs similarity index 92% rename from tests/operations/tests/BeautifyYAML.mjs rename to tests/operations/tests/JSONtoYAML.mjs index 348991d8..b18998e9 100644 --- a/tests/operations/tests/BeautifyYAML.mjs +++ b/tests/operations/tests/JSONtoYAML.mjs @@ -28,12 +28,12 @@ TestRegister.addTests([ ], }, { - name: "Beautify YAML", + name: "JSON to YAML", input: EXAMPLE_JSON, expectedOutput: `number: 3\nplain: string\n`, recipeConfig: [ { - op: "Beautify YAML", + op: "JSON to YAML", args: [], } ],