aboutsummaryrefslogtreecommitdiff
path: root/src/utility.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-12-10 11:10:47 -0800
committerAlon Zakai <alonzakai@gmail.com>2013-12-10 11:10:47 -0800
commit539ce7ac862e60a8ae3cd94c59a24a8e992b0c64 (patch)
treeeb4299c34bd112767b51df0715f21e37b7109549 /src/utility.js
parentd69977b56d12b5e8077b8e118b050ac288099a34 (diff)
quote keys in library objects when necessary; fixes #1908
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(',') + '}';
}
}