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)

Share Improve this question asked Apr 23, 2012 at 18:46 ThiefMasterThiefMaster 319k85 gold badges604 silver badges645 bronze badges 9
  • 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
 |  Show 4 more ments

1 Answer 1

Reset to default 12

This 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