From 829c07b13460de6b5ef788222ab25bf820a0764a Mon Sep 17 00:00:00 2001 From: n1073645 Date: Mon, 6 Apr 2020 16:06:48 +0100 Subject: [PATCH] JSONtoStr --- src/core/config/Categories.json | 1 + src/core/operations/JSONStringify.mjs | 47 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) create mode 100644 src/core/operations/JSONStringify.mjs diff --git a/src/core/config/Categories.json b/src/core/config/Categories.json index 77e3d319..66e3a78e 100755 --- a/src/core/config/Categories.json +++ b/src/core/config/Categories.json @@ -354,6 +354,7 @@ "JavaScript Minify", "JSON Beautify", "JSON Minify", + "JSON Stringify", "XML Beautify", "XML Minify", "SQL Beautify", diff --git a/src/core/operations/JSONStringify.mjs b/src/core/operations/JSONStringify.mjs new file mode 100644 index 00000000..689f0aab --- /dev/null +++ b/src/core/operations/JSONStringify.mjs @@ -0,0 +1,47 @@ +/** + * @author n1073645 [n1073645@gmail.com] + * @copyright Crown Copyright 2020 + * @license Apache-2.0 + */ + +import Operation from "../Operation.mjs"; +import OperationError from "../errors/OperationError.mjs"; + +/** + * JSON Stringify operation + */ +class JSONStringify extends Operation { + + /** + * JSONStringify constructor + */ + constructor() { + super(); + + this.name = "JSON Stringify"; + this.module = "Default"; + this.description = "The JSON.stringify() method converts a JavaScript object or value to a JSON string."; + this.infoURL = "https://w3schools.com/js/js_json_stringify.asp"; + this.inputType = "string"; + this.outputType = "string"; + this.args = []; + } + + /** + * @param {string} input + * @param {Object[]} args + * @returns {string} + */ + run(input, args) { + let result; + try { + result = JSON.parse(input); + } catch (err) { + throw new OperationError(err); + } + return JSON.stringify(result); + } + +} + +export default JSONStringify;