hussainahmad / as3corelib

Automatically exported from code.google.com/p/as3corelib
0 stars 0 forks source link

JSON cannot encode/decode date instances correctly #3

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
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.

Original issue reported on code.google.com by darron.schall on 29 Dec 2006 at 8:44

GoogleCodeExporter commented 8 years ago
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.

Original comment by mikechambers on 10 Jan 2007 at 4:51

GoogleCodeExporter commented 8 years ago
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

Original comment by mikechambers on 10 Jan 2007 at 5:07

GoogleCodeExporter commented 8 years ago
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)" 

Original comment by wmdmark on 1 May 2007 at 3:38

GoogleCodeExporter commented 8 years ago
BTW, I'd be happy to help :)

Original comment by wmdmark on 1 May 2007 at 3:39

GoogleCodeExporter commented 8 years ago
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

Original comment by kenlar...@gmail.com on 4 Jun 2008 at 10:32

GoogleCodeExporter commented 8 years ago
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.

Original comment by alan.dur...@citi.com on 5 Jun 2008 at 12:58

GoogleCodeExporter commented 8 years ago
are there news about it?

Original comment by mchiar...@gmail.com on 18 Feb 2009 at 11:22

GoogleCodeExporter commented 8 years ago
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

Original comment by nopl...@gmail.com on 15 Jun 2009 at 3:46

GoogleCodeExporter commented 8 years ago
Issue 94 has been merged into this issue.

Original comment by darron.schall on 3 Aug 2009 at 12:59

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

Original comment by gmariani405 on 21 Sep 2009 at 7:02