mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-20 06:55:08 -04:00
Added a JSON to YAML and a YALM to JSON operation
This commit is contained in:
parent
ae1b12c120
commit
23763363ba
5 changed files with 137 additions and 1 deletions
|
@ -63,7 +63,9 @@
|
|||
"JSON to CSV",
|
||||
"Avro to JSON",
|
||||
"CBOR Encode",
|
||||
"CBOR Decode"
|
||||
"CBOR Decode",
|
||||
"YAML to JSON",
|
||||
"JSON to YAML"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
46
src/core/operations/JSONToYAML.mjs
Normal file
46
src/core/operations/JSONToYAML.mjs
Normal file
|
@ -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;
|
45
src/core/operations/YAMLToJSON.mjs
Normal file
45
src/core/operations/YAMLToJSON.mjs
Normal file
|
@ -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;
|
|
@ -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
|
||||
|
|
42
tests/operations/tests/YAML.mjs
Normal file
42
tests/operations/tests/YAML.mjs
Normal file
|
@ -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: [],
|
||||
}
|
||||
],
|
||||
},
|
||||
]);
|
Loading…
Add table
Add a link
Reference in a new issue