Merge pull request #1286 from ccarpo/master

Added a JSON to YAML and a YAML to JSON operation
This commit is contained in:
a3957273 2025-04-03 08:19:20 +01:00 committed by GitHub
commit e469f00fc7
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 144 additions and 0 deletions

View file

@ -72,6 +72,8 @@
"Avro to JSON",
"CBOR Encode",
"CBOR Decode",
"YAML to JSON",
"JSON to YAML",
"Caret/M-decode",
"Rison Encode",
"Rison Decode",

View 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 = "Format a JSON object into 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;

View 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 = "Convert 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;