function stringify(obj_from_json) {
if (typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){
// not an object, stringify using native function
return JSON.stringify(obj_from_json);
}
// Implements recursive object serialization according to JSON spec
// but without quotes around the keys.
let props = Object
.keys(obj_from_json)
.map(key => `${key}:${stringify(obj_from_json[key])}`)
.join(",");
return `{${props}}`;
}
export const stringifyObjectWithNoQuotesOnKeys = (obj_from_json) => {
// In case of an array we'll stringify all objects.
if (Array.isArray(obj_from_json)) {
return `[${
obj_from_json
.map(obj => `${stringifyObjectWithNoQuotesOnKeys(obj)}`)
.join(",")
}]` ;
}
// not an object, stringify using native function
if(typeof obj_from_json !== "object" || obj_from_json instanceof Date || obj_from_json === null){
return JSON.stringify(obj_from_json);
}
// Implements recursive object serialization according to JSON spec
// but without quotes around the keys.
return `{${
Object
.keys(obj_from_json)
.map(key => `${key}:${stringifyObjectWithNoQuotesOnKeys(obj_from_json[key])}`)
.join(",")
}}`;
};
// \/\/\/ OH NOES!!! \/\/\/
if (typeof obj_from_json !== "object" || Array.isArray(obj_from_json)){
// not an object, stringify using native function
return JSON.stringify(obj_from_json);
// ^^^^^^^^^^^^^^ No! Don't do that!
}
function toUnquotedJSON(param){ // Implemented by Frostbolt Games 2022
if(Array.isArray(param)){ // In case of an array, recursively call our function on each element.
let results = [];
for(let elem of param){
results.push(toUnquotedJSON(elem));
}
return "[" + results.join(",") + "]";
}
else if(typeof param === "object"){ // In case of an object, loop over its keys and only add quotes around keys that aren't valid JavaScript variable names. Recursively call our function on each value.
let props = Object
.keys(param)
.map(function(key){
// A valid JavaScript variable name starts with a dollar sign (?), underscore (_) or letter (a-zA-Z), followed by zero or more dollar signs, underscores or alphanumeric (a-zA-Z\d) characters.
if(key.match(/^[a-zA-Z_$][a-zA-Z\d_$]*$/) === null) // If the key isn't a valid JavaScript variable name, we need to add quotes.
return `"${key}":${toUnquotedJSON(param[key])}`;
else
return `${key}:${toUnquotedJSON(param[key])}`;
})
.join(",");
return `{${props}}`;
}
else{ // For every other value, simply use the native JSON.stringify() function.
return JSON.stringify(param);
}
}