@kwiz/common
Version:
KWIZ common utilities and helpers for M365 platform
50 lines • 1.47 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.jsonParse = jsonParse;
exports.jsonStringify = jsonStringify;
exports.jsonStringifyNoQuotes = jsonStringifyNoQuotes;
const eval_1 = require("./eval");
const typecheckers_1 = require("./typecheckers");
function jsonParse(str) {
if ((0, typecheckers_1.isNullOrEmptyString)(str)) {
return null;
}
if (JSON) {
if ((0, typecheckers_1.isFunction)(JSON.parse)) {
try {
var v = JSON.parse(str);
return v;
}
catch (ex) {
return null;
}
}
}
try {
var v2 = (0, eval_1.eval2)("(" + str + ")");
return v2;
}
catch (ex1) {
return null;
}
}
/** stringify JSON object, but also sorts properties alphabetically */
function jsonStringify(obj, space) {
if ((0, typecheckers_1.isNullOrEmptyString)(obj))
return "";
var allKeys = [];
JSON.stringify(obj, (key, value) => {
if (!allKeys.includes(key))
allKeys.push(key);
return value;
});
allKeys.sort();
return JSON.stringify(obj, allKeys, space);
}
/** stringify json object without quotes on property names */
function jsonStringifyNoQuotes(obj) {
return jsonStringify(obj, 2).replace(/^[\t ]*"[^:\n\r]+(?<!\\)":/gm, function (match) {
return match.replace(/"/g, "");
});
}
//# sourceMappingURL=json.js.map