Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CC-1523: YAML-JSON conversion #1564

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"xmldom": "^0.6.0",
"xpath": "0.0.32",
"xregexp": "^5.1.1",
"yaml": "^2.2.1",
"zlibjs": "^0.3.1"
},
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/core/config/Categories.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@
"Parse TLV",
"CSV to JSON",
"JSON to CSV",
"YAML to JSON",
"JSON to YAML",
"Avro to JSON",
"CBOR Encode",
"CBOR Decode"
Expand Down
48 changes: 48 additions & 0 deletions src/core/operations/JSONToYAML.mjs
Original file line number Diff line number Diff line change
@@ -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":`));
Copy link

@DerpgonCz DerpgonCz Nov 20, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am quite sure this would not work, at all.

Wouldn't it be easier to use YAML.stringify(input)?

To clarify why:
The input is a JSON, why change it? This just quotes everything before a :. It breaks on this completely valid JSON, for example:

{"key":"value: some value"}

Try it in your browser console:

(() => {
    const input = '{"key":"value: some value"}';
    return 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;
53 changes: 53 additions & 0 deletions src/core/operations/YAMLToJSON.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @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";

/**
* YAML To JSON operation
*/
class YAMLToJSON extends Operation {

/**
* YAMLToJSON constructor
*/
constructor() {
super();

this.name = "YAML to JSON";
this.module = "Default";
this.description = "Converts YAML data to a JSON based on the definition in RFC 4180.";
this.infoURL = "https://en.wikipedia.org/wiki/JSON";
this.inputType = "string";
this.outputType = "JSON";
this.args = [
{
name: "Use Spaces",
type: "number",
value: 0
}
];
}

/**
* @param {YAML} input
* @param {Object[]} args
* @returns {string}
*/
run(input, args) {
const [spaces] = args;
try {
return JSON.stringify(YAML.parseDocument(input).toJSON(), null, spaces);
} catch (err) {
throw new OperationError("Unable to parse YAML To JSON: " + err.toString());
}
}

}

export default YAMLToJSON;
36 changes: 36 additions & 0 deletions tests/operations/tests/JSONtoYAML.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/**
* JSON to YAML tests.
*
* @author mshwed [m@ttshwed.com]
*
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

const EXPECTED_YAML = "version: 1.0.0\ndependencies:\n yaml: ^1.10.0\npackage:\n exclude:\n - .idea/**\n - .gitignore\n";

TestRegister.addTests([
{
name: "JSON to YAML: no spacing",
input: JSON.stringify({ "version": "1.0.0", "dependencies": { "yaml": "^1.10.0" }, "package": { "exclude": [".idea/**", ".gitignore"]}}),
expectedOutput: EXPECTED_YAML,
recipeConfig: [
{
op: "JSON to YAML",
args: []
},
],
},
{
name: "JSON to YAML: with spacing",
input: JSON.stringify({ "version": "1.0.0", "dependencies": { "yaml": "^1.10.0" }, "package": { "exclude": [".idea/**", ".gitignore"]}}, null, 4),
expectedOutput: EXPECTED_YAML,
recipeConfig: [
{
op: "JSON to YAML",
args: []
},
],
}
]);
37 changes: 37 additions & 0 deletions tests/operations/tests/YAMLtoJSON.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* YAML to JSON tests.
*
* @author mshwed [m@ttshwed.com]
*
* @copyright Crown Copyright 2019
* @license Apache-2.0
*/
import TestRegister from "../../lib/TestRegister.mjs";

const EXPECTED_JSON_SINGLE = '{"version":"1.0.0","dependencies":{"yaml":"^1.10.0"},"package":{"exclude":[".idea/**",".gitignore"]}}';
const EXPECTED_JSON_SPACED_SINGLE = '{\n"version": "1.0.0",\n"dependencies": {\n "yaml": "^1.10.0"\n },\n"package": {\n "exclude": [\n ".idea/**",\n ".gitignore"\n ]\n }\n}';

TestRegister.addTests([
{
name: "YAML to JSON: simple",
input: "version: 1.0.0\ndependencies:\n yaml: ^1.10.0\npackage:\n exclude:\n - .idea/**\n - .gitignore\n",
expectedOutput: EXPECTED_JSON_SINGLE,
recipeConfig: [
{
op: "YAML to JSON",
args: [0]
},
],
},
{
name: "YAML to JSON: spacing",
input: "version: 1.0.0\ndependencies:\n yaml: ^1.10.0\npackage:\n exclude:\n - .idea/**\n - .gitignore\n",
expectedOutput: EXPECTED_JSON_SPACED_SINGLE,
recipeConfig: [
{
op: "YAML to JSON",
args: [2]
},
],
}
]);
Loading