9

I'm serializing objects to JSON strings with JavaScript,

I noticed only enumerable object properties 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)); //result is {"y":"hello"}

[pen]

I'm wondering why that is? I've searched through the MDN page, the json2 parser documentation. I could not find this behavior documented any-where.

I suspect this is the result of using for... in loops that only go through [[enumerable]] properties (at least in the case of json2). This can probably be done with something like Object.getOwnPropertyNames that returns both enumerable, and non-enumerable properties. That might be problematic to serialize though (due to deserialization).

tl;dr

  • Why does JSON.stringify only serialize enumerable properties?
  • Is this behavior documented anywhere?
  • How can I implement serializing non-enumerable properties myself?
Benjamin Gruenbaum
  • 260,410
  • 85
  • 489
  • 489
  • If you set enumerable to false, there will be no instance added. Try console logging the object before you stringify it, and you'll see that `x` is missing from the object itself, so it surely can't be there once it's stringified. – adeneo Mar 31 '13 at 20:08
  • 1
    @adeneo that's presumably because it also uses something like stringify and not getOwnPropertyNames. Object.getOwnPropertyNames(a) returns ["x","y"] and if you type obj.x you get "hello". – Benjamin Gruenbaum Mar 31 '13 at 20:10
  • Yes I've noticed, and that's a good indication that you should probably reconsider and find another way to create your object. – adeneo Mar 31 '13 at 20:12
  • 1
    @adeneo Enumerable properties serve a purpose. In the very basic sample input absolutely. However, this question is a _very reduced_ case of what I'm actually doing. Thanks for the input and time. – Benjamin Gruenbaum Mar 31 '13 at 20:15
  • @BenjaminGruenbaum - As a side note for `Object.create`, enumerable will default to false unless explicitly defined. Here is a sample jsfiddle showing the behavior: http://jsfiddle.net/T3PGD/2/ – Travis J Mar 31 '13 at 20:23

2 Answers2

13

It's specified in the ES5 spec.

If Type(value) is Object, and IsCallable(value) is 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.

So, let's look at JO. Here's the relevant section:

Let K be an internal List of Strings consisting of the names of all the own properties of value whose [[Enumerable]] attribute is true. The ordering of the Strings should be the same as that used by the Object.keys standard built-in function.

ThiefMaster
  • 298,938
  • 77
  • 579
  • 623
0

As @ThiefMaster answered above, it's specified in the spec

however, if you know the names of the non-enumerable properties you like to be serialized ahead of time, you can achieve it by passing a replacer function as the second param to JSON.stringify() (documentation on MDN), like so

var o = {
  prop: 'propval',
}

Object.defineProperty(o, 'propHidden', {
  value: 'propHiddenVal',
  enumerable: false,
  writable: true,
  configurable: true
});

var s = JSON.stringify(o, (key, val) => {
  if (!key) {
    // Initially, the replacer function is called with an empty string as key representing the object being stringified. It is then called for each property on the object or array being stringified.
    if (typeof val === 'object' && val.hasOwnProperty('propHidden')) {
      Object.defineProperty(val, 'propHidden', {
        value: val.propHidden,
        enumerable: true,
        writable: true,
        configurable: true
      });

    }
  }
  return val;
});

console.log(s);
dalimian
  • 1,696
  • 1
  • 10
  • 6
  • 1
    Sadly, with this solution the non-enumerable properties are enumerable after the serialization. – Chris K Aug 23 '19 at 14:44