admin管理员组文章数量:1287511
At least in Firefox, you can stringify a Date object:
>>> JSON.stringify({'now': new Date()})
'{"now":"2012-04-23T18:44:05.600Z"}'
This works because (in Firefox) Date
contains a toJSON
method which is used by its JSON serializer. However, this is not part of the JSON standard so I wonder why this method exists or rather why the builtin JSON serializer checks for such a method. Since it's not standardized you cannot safely use it anyway without first testing if the builtin serializer understands it and otherwise use a custom one (such as json2.js)
At least in Firefox, you can stringify a Date object:
>>> JSON.stringify({'now': new Date()})
'{"now":"2012-04-23T18:44:05.600Z"}'
This works because (in Firefox) Date
contains a toJSON
method which is used by its JSON serializer. However, this is not part of the JSON standard so I wonder why this method exists or rather why the builtin JSON serializer checks for such a method. Since it's not standardized you cannot safely use it anyway without first testing if the builtin serializer understands it and otherwise use a custom one (such as json2.js)
- 5 It is part of the ECMAScript standard. – ChaosPandion Commented Apr 23, 2012 at 18:48
- it also exists in Chrome: Date.prototype.toJSON – Kamyar Nazeri Commented Apr 23, 2012 at 18:49
- I thought it might be calling toString, but I tried that and you get a different format of string. – mystery Commented Apr 23, 2012 at 18:49
- 1 @ChaosPandion: Post it as a answer (e.g. with a quote from the ES standard) and I'll accept it. Just checked that document and it indeed defines how to serialize unknown objects. – ThiefMaster Commented Apr 23, 2012 at 18:50
- 1 You should most certainly use ISO-8601 whenever converting a date to a string. At that point, however, what you are serializing is a string, not a Date object... and JSON supports strings just fine. – Mark Reed Commented Apr 23, 2012 at 19:06
1 Answer
Reset to default 12This works because it is specified in a not so clear matter within the specification. Starting out you need to dig in into section 15.12.3 in the description of the abstract operation Str which is used to convert values to a string representation. Essentially if the input is an object the specification says to check for the existance of a callable value named toJSON
. Think of this like an interface in Java or C#.
interface IAmJSON
{
string toJSON(string key);
}
This is the exact text from the specification.
2. If Type(value) is Object, then a. Let toJSON be the result of calling the [[Get]] internal method of value with argument "toJSON". b. If IsCallable(toJSON) is true i. Let value be the result of calling the [[Call]] internal method of toJSON passing value as the this value and with an argument list consisting of key.
Finally, the date object has toJSON
defined in section 15.9.5.44.
本文标签: javascriptWhy does JSONstringify() accept Date objectsStack Overflow
版权声明:本文标题:javascript - Why does JSON.stringify() accept Date objects? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741272936a2369567.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论