te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Why does JSON.stringify not serialize non-enumerable properties? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Why does JSON.stringify not serialize non-enumerable properties? - Stack Overflow

programmeradmin3浏览0评论

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?

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?
Share Improve this question asked Mar 31, 2013 at 20:01 Benjamin GruenbaumBenjamin Gruenbaum 276k89 gold badges518 silver badges514 bronze badges 5
  • 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 Commented Mar 31, 2013 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 Commented Mar 31, 2013 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 Commented Mar 31, 2013 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 Commented Mar 31, 2013 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: jsfiddle/T3PGD/2 – Travis J Commented Mar 31, 2013 at 20:23
Add a ment  | 

3 Answers 3

Reset to default 13

It's specified in the ES5 spec.

  1. If Type(value) is Object, and IsCallable(value) is false

    • a. If the [[Class]] internal property of value is "Array" then

      • i. Return the result of calling the abstract operation JA with argument value.
    • b. 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.

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);

You can achieve a generic JSON.stringify() that includes non-enumerables with code like below. But first, some notes:

  • This code has not been thoroughly tested for possible bugs or unexpected behavior; also, it turns functions into basic objects. Treat accordingly.

  • copyEnumerable() is the function to pay attention to. It does not itself internally call JSON.stringify() (you can do that yourself, with the output object) mainly because recursive calls to JSON.stringify() (due to nested objects) will yield uglier, unwanted results.

  • I'm only checking for object and function types to be treated specially; I believe that these are the only types which need to be handled specially... Note that JS Arrays ([]) fall into this category (objects), as well as classes (functions), and null does not. Since null is of type object, I included the !! check.

  • The stopRecursiveCopy Set is just to make sure it's not doing infinite recursion.

  • I'm using a trick with the 2 extra parameters to JSON.stringify() calls to make it output something formatted prettier, for easy reading.


The code is in an easy format to try out and tweak in the console:

{
  o = {};
  d = {};
  Object.defineProperties(o, {
    a: {
      value: 5,
      enumerable: false
    },
    b: {
      value: "test",
      enumerable: false
    },
    c: {
      value: {},
      enumerable: false
    }
  });
  Object.defineProperty(o.c, "d", {
    value: 7,
    enumerable: false
  });

  //The code to use (after careful consideration!).
  function isObject(testMe) {
    return ((typeof(testMe) === "object" && !!testMe) ||
      typeof(testMe) === "function");
  }
  let stopRecursiveCopy = new Set();
  function copyEnumerable(obj) {
    if (!isObject(obj)) {
      return obj;
    }
    let enumerableCopy = {};
    for (let key of Object.getOwnPropertyNames(obj)) {
      if (isObject(obj[key])) {
        if (!stopRecursiveCopy.has(obj[key])) {
          stopRecursiveCopy.add(obj[key]);
          enumerableCopy[key] = copyEnumerable(obj[key]);
          stopRecursiveCopy.delete(obj[key]);
        }
      } else {
        enumerableCopy[key] = obj[key];
      }
    }
    return enumerableCopy;
  }

  console.log(JSON.stringify(copyEnumerable(o), null, "  "));

  Object.defineProperty(copyEnumerable, "test", {
    value: 10,
    enumerable: false
  });
  console.log(JSON.stringify(copyEnumerable(copyEnumerable), null, "  "));

  Object.defineProperty(o, "f", {
    value: copyEnumerable,
    enumerable: false
  });
  console.log(JSON.stringify(copyEnumerable(o), null, "  "));
}

which outputs:

//o
{
  "a": 5,
  "b": "test",
  "c": {
    "d": 7
  }
}

//copyEnumerable itself
{
  "test": 10,
  "prototype": {
    "constructor": {
      "test": 10,
      "length": 1,
      "name": "copyEnumerable"
    }
  },
  "length": 1,
  "name": "copyEnumerable"
}

//o again, but with `f: copyEnumerable` added
{
  "a": 5,
  "b": "test",
  "c": {
    "d": 7
  },
  "f": {
    "test": 10,
    "prototype": {},
    "length": 1,
    "name": "copyEnumerable"
  }
}

Pertinent links:

  • https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperties https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/defineProperty
  • https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/getOwnPropertyNames
  • https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Operators/typeof
  • https://stackoverflow./a/14706877/1599699
  • https://stackoverflow./a/18538851/1599699
发布评论

评论列表(0)

  1. 暂无评论