I am serializing objects for JSON strings with JavaScript,
I noticed that only the enumerated properties of the object get serialized:
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));
[ pen ]
I wonder why this is? I looked at the json2 documentation page. I could not find this behavior to be documented anywhere.
I suspect this is the result of using for... in loops that only execute the [[enumerated]] properties (at least in the case of json2 ). This can probably be done using Object.getOwnPropertyNames , which returns both enumerated and non-enumerated properties. This can be problematic for serialization (due to deserialization).
TL; DR
- Why
JSON.stringify only serialize enumerated properties? - Is this behavior documented anywhere?
- How can I serialize non-enumerable properties on my own?
source share