-
Notifications
You must be signed in to change notification settings - Fork 3.3k
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
depperm
wants to merge
5
commits into
gchq:master
Choose a base branch
from
depperm:CC-1523
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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":`)); | ||
return doc.toString(); | ||
} catch (err) { | ||
throw new OperationError("Unable to parse JSON to YAML: " + err.toString()); | ||
} | ||
} | ||
|
||
} | ||
|
||
export default JSONToYAML; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: [] | ||
}, | ||
], | ||
} | ||
]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] | ||
}, | ||
], | ||
} | ||
]); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:Try it in your browser console: