Ben*_*aum 7 javascript serialization json
我正在使用JavaScript将对象序列化为JSON字符串,
我注意到只有可枚举的对象属性被序列化:
var a = Object.create(null,{
x: { writable:true, configurable:true, value: "hello",enumerable:false },
y: { writable:true, configurable:true, value: "hello",enumerable:true }
});
document.write(JSON.stringify(a)); //result is {"y":"hello"}
Run Code Online (Sandbox Code Playgroud)
[ 笔 ]
我想知道为什么会这样?我搜索了MDN页面,json2解析器文档.我无法在任何地方找到这种行为.
我怀疑这是使用仅通过[[enumerable]]属性的for... in循环的结果(至少在这种情况下).这可以通过类似的东西来完成,同时返回可枚举和不可枚举的属性.这可能是序列化的问题(由于反序列化).json2Object.getOwnPropertyNames
TL;博士
JSON.stringify只序列化可枚举的属性?它在ES5规范中指定.
如果Type(value)是Object,则IsCallable(value)为false
If the [[Class]] internal property of value is "Array" then
Return the result of calling the abstract operation JA with argument value.
Else, return the result of calling the abstract operation JO with argument value.
Run Code Online (Sandbox Code Playgroud)
那么,让我们来看看JO.这是相关部分:
设K是一个内部字符串列表,由[[Enumerable]]属性为true的所有值属性的名称组成.字符串的顺序应与Object.keys标准内置函数使用的顺序相同.