aboutsummaryrefslogtreecommitdiff
path: root/src/utility.js
diff options
context:
space:
mode:
Diffstat (limited to 'src/utility.js')
-rw-r--r--src/utility.js8
1 files changed, 7 insertions, 1 deletions
diff --git a/src/utility.js b/src/utility.js
index cd27b209..178c596b 100644
--- a/src/utility.js
+++ b/src/utility.js
@@ -346,13 +346,19 @@ function sortedJsonCompare(x, y) {
return true;
}
+function escapeJSONKey(x) {
+ if (/^[\d\w_]+$/.exec(x) || x[0] === '"' || x[0] === "'") return x;
+ assert(x.indexOf("'") < 0, 'cannot have internal single quotes in keys: ' + x);
+ return "'" + x + "'";
+}
+
function stringifyWithFunctions(obj) {
if (typeof obj === 'function') return obj.toString();
if (obj === null || typeof obj !== 'object') return JSON.stringify(obj);
if (isArray(obj)) {
return '[' + obj.map(stringifyWithFunctions).join(',') + ']';
} else {
- return '{' + keys(obj).map(function(key) { return key + ':' + stringifyWithFunctions(obj[key]) }).join(',') + '}';
+ return '{' + keys(obj).map(function(key) { return escapeJSONKey(key) + ':' + stringifyWithFunctions(obj[key]) }).join(',') + '}';
}
}