JSON.stringify and Date serialization issues

Erik Arvidsson erik.arvidsson at gmail.com
Tue Aug 12 10:10:56 PDT 2008


The issue with representing a Date as a string in JSON has been
discussed before and the resolution was that stringify a Date object
to the ISO date string was considered not desirable.  However, in the
latest proposed spec for ES3.1 this resolution seems to have been
overlooked.

The problem is that if Date is serialized as a string then there is no
longer a way to distinguish a string from a date object.  It is
therefore better not to have any special handling of Date.  Even with
the currently proposed spec for JSON one would on first glance believe
that it would be possible to use the replacer argument to handle Dates
in a way that might allow them to be parsed again without loosing
information but the algorithm states that toJSON is called on the
object before it is passed to the replacer function.

I can see 2 possible ways to resolve this.

1. Drop toJSON on Date.prototype.
2. Change how the replacer is called and pass it both the key,
non-json-value and the json value.  This would allow the user to
detect a Date object and return whatever he/she wants.

--
erik



More information about the Es-discuss mailing list