Bug 1250191 - Address rnewman review, r=rnewman
MozReview-Commit-ID: GfWytXLRj9X
--- a/services/common/canonical-json.js
+++ b/services/common/canonical-json.js
@@ -15,53 +15,45 @@ this.CanonicalJSON = {
*
* @param source
* The elements to be serialized.
*
* The output will have all unicode chars escaped with the unicode codepoint
* as lowercase hexadecimal.
*
* @usage
- * getCanonicalJSON(listOfRecords);
+ * CanonicalJSON.stringify(listOfRecords);
**/
stringify: function stringify(source) {
if (Array.isArray(source)) {
const jsonArray = source.map(x => typeof x === "undefined" ? null : x);
return `[${jsonArray.map(stringify).join(",")}]`;
}
if (typeof source === "number") {
if (source === 0) {
- return (1/source) > 0 ? "0" : "-0";
+ return (Object.is(source, -0)) ? "-0" : "0";
}
}
+ // Leverage jsesc library, mainly for unicode escaping.
+ const toJSON = (input) => jsesc(input, {lowercaseHex: true, json: true});
+
if (typeof source !== "object" || source === null) {
return toJSON(source);
}
-
// Dealing with objects, ordering keys.
const sortedKeys = Object.keys(source).sort();
+ const lastIndex = sortedKeys.length - 1;
return sortedKeys.reduce((serial, key, index) => {
const value = source[key];
// JSON.stringify drops keys with an undefined value.
if (typeof value === "undefined") {
return serial;
}
const jsonValue = value && value.toJSON ? value.toJSON() : value;
- const suffix = index !== sortedKeys.length - 1 ? "," : "";
+ const suffix = index !== lastIndex ? "," : "";
const escapedKey = toJSON(key);
return serial + `${escapedKey}:${stringify(jsonValue)}${suffix}`;
}, "{") + "}";
- }
-}
-
-/**
- * Encode the given input to valid escaped JSON.
- * @private
- **/
-function toJSON(input) {
- return jsesc(input, {
- lowercaseHex: true,
- json: true
- });
-}
+ },
+};
--- a/services/common/tests/unit/test_canonicaljson.js
+++ b/services/common/tests/unit/test_canonicaljson.js
@@ -1,12 +1,12 @@
/* Any copyright is dedicated to the Public Domain.
http://creativecommons.org/publicdomain/zero/1.0/ */
-Cu.import("resource://services-common/canonical-json.js");
+const { CanonicalJSON } = Cu.import("resource://services-common/canonical-json.js");
function stringRepresentation(obj) {
const clone = JSON.parse(JSON.stringify(obj));
return JSON.stringify(clone);
}
add_task(function* test_canonicalJSON_should_preserve_array_order() {
const input = ['one', 'two', 'three'];