Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Last active May 5, 2017 05:38
Show Gist options
  • Save leizongmin/5011737 to your computer and use it in GitHub Desktop.
Save leizongmin/5011737 to your computer and use it in GitHub Desktop.
格式化输出JSON字符串
/**
* 返回安全的JSON字符串
*
* @param {Object} data
* @param {String|Number} space 缩进
* @return {String}
*/
var jsonStringify = function (data, space) {
var seen = [];
return JSON.stringify(data, function (key, val) {
if (!val || typeof val !== 'object') {
return val;
}
if (seen.indexOf(val) !== -1) {
return '[Circular]';
}
seen.push(val);
return val;
}, space);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment