Created
April 6, 2012 10:38
-
-
Save jcayzac/2318775 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var sortedJSON = function (obj) { | |
if (obj === null) { | |
return 'null'; | |
} | |
if (Array.isArray(obj)) { | |
return '[' + obj.map(sortedJSON).join(',') + ']'; | |
} | |
if (obj instanceof RegExp) { | |
return sortedJSON(obj.source); | |
} | |
if (typeof obj === "object" && !(obj instanceof Date)) { | |
return '{' + Object.keys(obj).sort().map(function (key) { | |
return [key, sortedJSON(obj[key])]; | |
}).filter(function (tuple) { | |
return typeof tuple[1] !== "undefined"; | |
}).map(function (tuple) { | |
return '"' + tuple[0] + '": ' + tuple[1]; | |
}).join(', ') + '}'; | |
} | |
return JSON.stringify(obj); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment