-
Notifications
You must be signed in to change notification settings - Fork 193
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
64.深度使用 JSON.stringify() #69
Comments
推荐看我的这篇文章:JSON |
使用JSON.stringify()和JSON.parse()做对象的深拷贝:
|
@xwHoward 所提到的深拷贝方法会忽略undefined和function,所以需要采用文章的方法再次封装 - - |
@xwHoward 这种深拷贝方式有什么副作用吗 |
直接用扩展运算符 |
@sakuyakun 副作用就是文中提到的undefined和function会忽略咯,还有就是不支持JSON的浏览器就用不了 |
比较有意思 |
@lulusir 还有如果字符串中含有'\n',JSON.parse解析是会报错的 |
@honpery @sakuyakun @lulusir @xxyj 确实这种方法有一定局限性,但在对数据结构明了、性能要求比较高(相比用递归进行对象复制)的时候可以用这种方式快速进行对象深拷贝,算是一种trick啦 |
很好 |
赞 可以的 |
@xwHoward 按我的了解,该方法复制对象的性能比直接使用传统方式复制对象更慢。
|
@lulusir 瀏覽器支援不是問題了。 |
var myMap = new Map();
myMap.set(0, 'zero');
var mySet = new Set();
mySet.add(1);
class test {
constructor(opt) {
this.opt = opt;
}
}
JSON.stringify({
String: 'string',
Boolean: true,
Number: 123,
NaN: NaN, // null,
Infinity: Infinity, // null
null: null, // null
undefined: undefined, // 没有 key
Array: [1, 2, 3],
Object: {
foo: 'bar',
},
Symbol: Symbol('foo'), // {}
Map: myMap, // {}
Set: mySet, // {}
Promise: new Promise(function(resolve, reject) {}), // {}
Proxy: new Proxy({}, {}), // {}
Class: test, // 没有 key
ClassA: new test(123), // { "opt": 123 },
Math: Math, // {}
// Buffer: new Buffer('123'), // nodeJs '{"Buffer":{"type":"Buffer","data":[49,50,51]}}'
Error: new Error('error'), // {}
Function: function() { // 没有 key
console.info('hi');
},
Date: new Date(), // "2017-08-01T13:52:48.628Z",
RegExp: /RegExp/, // {}
}, null, 2);
/*
"{
"String": "string",
"Boolean": true,
"Number": 123,
"NaN": null,
"Infinity": null,
"null": null,
"Array": [
1,
2,
3
],
"Object": {
"foo": "bar"
},
"Map": {},
"Set": {},
"Promise": {},
"Proxy": {},
"ClassA": {
"opt": 123
},
"Math": {},
"Error": {},
"Date": "2017-08-01T14:04:58.522Z",
"RegExp": {}
}"
*/ |
深度使用 JSON.stringify()
按照 JSON 的规范,使用 JSON.stringify() 做对象序列化时,如果一个属性为函数,那这个属性就会被忽略。
还有一种情况,一个属性的值为
undefined
如果属性为
null
则可以正常序列化这个属性:因为
null
可表示已经赋值,而undefined
表示未定义、未赋值,所以执行JSON.stringify
不会处理。stringify
函数stringify
函数的定义为JSON.stringify(value [, replacer [, space]])
后面还带有我不常用两个可选参数 replacer 和 space
value
参数不多解释,replacer
其实就是一个自定义函数,可以改变JSON.stringify
的行为,space
就是格式化输出,最大值为 10,非整数时取值为 1stringify
输出Function
本质上无论怎么改,
stringify
还是不会输出Function
,但是Function
可以调用toString()
方法的,所以思路就很明了了。同理可证
undefined
也能输出了stringify
格式化输出JSON.stringify
的第三个参数很简单,相当于我们编辑器的tab_size
toJSON
方法toJSON
是个覆盖函数,尽量少用,看了代码就懂了:直接覆盖掉所有的序列化处理,返回了 "WTF"
下面的评论中 @yingyuk 给出了详细的 stringify转换demo
The text was updated successfully, but these errors were encountered: