Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON cannot encode/decode date instances correctly #5

Open
darronschall opened this issue Jul 28, 2010 · 11 comments
Open

JSON cannot encode/decode date instances correctly #5

darronschall opened this issue Jul 28, 2010 · 11 comments

Comments

@darronschall
Copy link
Contributor

Originally filed by darron.schall on 2006-12-29T20:44:29

What steps will reproduce the problem?

  1. trace( JSON.encode( new Date() ) );

What is the expected output? What do you see instead?

The expected output, unfortunately, is not that well defined. There is no
standard JSON syntax for dealing with dates. A proposed standard is here:

http://www.nikhilk.net/DateSyntaxForJSON2.aspx

Essentially, serializing the date to "@Number@" where number is the number
of milliseconds that have passed since Jan 1, 1970.

Either that, or we can use one of the ISO Date Standard formats.

The output from the trace statement is simply {}. Clearly an empty object
is not good, not matter what the expected output should be.

Please use labels and text to provide additional information.

Whatever sort of serialization we implement should probably be specified
via a flag, passed to the static encode/decode methods. Something like:

JSON.encode( date, JSON.DATE_FORMAT_ISO_8601 );
or
JSON.encode( date, JSON.DATE_FORMAT_AT_MILLIS_AT );

Also, a the decode routine should be specified so that string matching the
encoding format will be converted into true Date instances.

@darronschall
Copy link
Contributor Author

Updated by mikechambers on 2007-01-10T04:51:23

Here is the RFC:

http://www.ietf.org/rfc/rfc4627.txt?number=4627

I cant find any mention of dates.

Here is a page from mozilla's thoughts on EcmaScript 4 and JSON:

http://developer.mozilla.org/es4/proposals/json_encoding_and_decoding.html

Specifically, it mentions:

*
  Date.prototype.toJSONString()

Returns a String containing the representation of a date. A Date object is serialized

as an ISO date string in double quotes.

So, I think it is very important that we follow whatever the "standard" is. There are
already alternate implementations, which could result in incompatible versions of JSON.

I will ping Douglas Crockford, and see if they have any definitive thoughts on date
serialization in JSON.

@darronschall
Copy link
Contributor Author

Updated by mikechambers on 2007-01-10T05:07:34

The latest JSON.js file (from December 6, 2006), has support for Dates:

http://www.json.org/json.js

It uses ISO 8601

http://www.iso.org/iso/en/prods-services/popstds/datesandtime.html

We have some Data format functions in DateUtils

http://as3corelib.googlecode.com/svn/trunk/src/com/adobe/utils/DateUtil.as

but unfortunately, dont have ISO 8601

@darronschall
Copy link
Contributor Author

Updated by wmdmark on 2007-05-01T03:38:49

Are there any work arounds for this? I'm needing to parse dates from a JSON feed in
a project I'm working on. The date value comes in from the server as "new Date(-
59011459200000)"

@darronschall
Copy link
Contributor Author

Updated by wmdmark on 2007-05-01T03:39:45

BTW, I'd be happy to help :)

@darronschall
Copy link
Contributor Author

Updated by kenlarkin on 2008-06-04T10:32:25

until you resolve this you add this to JSONEncoder.as when checking the object o.

else if (o is Date)
{
s = "new Date(" + o.time.toString() + ")";
return s;
}
else // o is a class instance

@darronschall
Copy link
Contributor Author

Updated by [email protected] on 2008-06-05T12:58:12

It seems that a lot of implementations of JSON are using proprietary date formats to
get around this issue. This kind of defeats the interoperability aim of JSON. I
would suggest that providing a default serializer/deserializer for dates with the
ability to insert or use a different one would be useful. In other words, provide an
interface like
SerializeDate(param:Date):String
IsDate(param:String):bool
DeserializeDate(param:String):Date
where the interface could be changed if the server side technology uses a different
format.
Like wndmark, I'd be happy to help too.

@darronschall
Copy link
Contributor Author

Updated by mchiareli on 2009-02-18T23:22:39

are there news about it?

@darronschall
Copy link
Contributor Author

Updated by noplace on 2009-06-15T03:46:36

Dear all,
I was in need to decode JSON coming from a .NET web service and as3corelib was the
only solution I found, and after finding out that it does not decode the date I took
the JSON source files and put them in my own lib and modified the code to check for
the date string MS sends as json "/Date(...)/" and returns a proper date object.

if anyone is intrested in this let me know.

thanks

@darronschall
Copy link
Contributor Author

Updated by darron.schall on 2009-08-03T00:59:21

Issue 94 has been merged into this issue.

@darronschall
Copy link
Contributor Author

Updated by gmariani405 on 2009-09-21T19:02:12

Is any progress being made to this? It's been an issue for going on 3 years.

@seangenung
Copy link

Darron,

I could really use that .net implementation you worked up, could you post some code on how you handled it. Is anyone working on adding this to as3corelib?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants