mirror of
https://github.com/gchq/CyberChef.git
synced 2025-04-23 00:06:17 -04:00
CC-1523: YAML-JSON conversion
This commit is contained in:
parent
1bc88728f0
commit
c8e306d4f4
7 changed files with 202 additions and 6 deletions
48
src/core/operations/JSONToYAML.mjs
Normal file
48
src/core/operations/JSONToYAML.mjs
Normal file
|
@ -0,0 +1,48 @@
|
|||
/**
|
||||
* @author n1474335 [n1474335@gmail.com]
|
||||
* @copyright Crown Copyright 2018
|
||||
* @license Apache-2.0
|
||||
*/
|
||||
|
||||
import YAML from 'yaml'
|
||||
import Operation from "../Operation.mjs";
|
||||
import OperationError from "../errors/OperationError.mjs";
|
||||
|
||||
/**
|
||||
* JSON to YAML operation
|
||||
*/
|
||||
class JSONToYAML extends Operation {
|
||||
|
||||
/**
|
||||
* JSONToYAML constructor
|
||||
*/
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.name = "JSON to YAML";
|
||||
this.module = "Default";
|
||||
this.description = "Converts JSON data to a YAML.";
|
||||
this.infoURL = "https://yaml.org/spec/1.2.2/";
|
||||
this.inputType = "string";
|
||||
this.outputType = "string";
|
||||
this.args = [];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {JSON} input
|
||||
* @param {Object[]} args
|
||||
* @returns {string}
|
||||
*/
|
||||
run(input, args) {
|
||||
const doc = new YAML.Document();
|
||||
try {
|
||||
doc.contents = JSON.parse(input.replace(/(\w+):/gm, `"$1":`));
|
||||
return doc.toString()
|
||||
} catch (err) {
|
||||
throw new OperationError("Unable to parse JSON to YAML: " + err.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export default JSONToYAML;
|
Loading…
Add table
Add a link
Reference in a new issue