datetime.date is not JSON serializable

1,970 views
Skip to first unread message

carlo

unread,
Jan 14, 2009, 12:04:18 PM1/14/09
to web2py Web Framework
I had this problem trying to serialize a (SQLite) date field with
simplejson through "js.dumps(mydata)" :

File "C:\Python25\web2py\gluon\contrib\simplejson\encoder.py", line
332, in default
raise TypeError("%r is not JSON serializable" % (o,))
TypeError: datetime.date(2009, 1, 14) is not JSON serializable

It seems something similar happened in Django:

http://code.djangoproject.com/ticket/2460

Is there any workaround?

carlo

Timothy Farrell

unread,
Jan 14, 2009, 12:18:17 PM1/14/09
to web...@googlegroups.com
That's funny, I just dealt with this issue yesterday. I'll put a tip on
the wiki and link it.

-tim
--
Timothy Farrell <tfar...@swgen.com>
Computer Guy
Statewide General Insurance Agency (www.swgen.com)

carlo

unread,
Jan 14, 2009, 12:24:18 PM1/14/09
to web2py Web Framework
Thank you Tim, I am looking forward to it.

carlo

On 14 Gen, 18:18, Timothy Farrell <tfarr...@swgen.com> wrote:
> That's funny, I just dealt with this issue yesterday.  I'll put a tip on
> the wiki and link it.
>
> -tim
>
>
>
> carlo wrote:
> > I had this problem trying to serialize a (SQLite) date field with
> > simplejson  through "js.dumps(mydata)" :
>
> > File "C:\Python25\web2py\gluon\contrib\simplejson\encoder.py", line
> > 332, in default
> >     raise TypeError("%r is not JSON serializable" % (o,))
> > TypeError: datetime.date(2009, 1, 14) is not JSON serializable
>
> > It seems something similar happened in Django:
>
> >http://code.djangoproject.com/ticket/2460
>
> > Is there any workaround?
>
> > carlo
>
> --
> Timothy Farrell <tfarr...@swgen.com>

Timothy Farrell

unread,
Jan 14, 2009, 12:35:56 PM1/14/09
to web...@googlegroups.com

mdipierro

unread,
Jan 14, 2009, 12:43:20 PM1/14/09
to web2py Web Framework
fantastic. can you email me a patch?
does it work for date and time too?

carlo

unread,
Jan 14, 2009, 12:46:52 PM1/14/09
to web2py Web Framework
very interesting, thank you!

carlo

Timothy Farrell

unread,
Jan 14, 2009, 1:39:49 PM1/14/09
to web...@googlegroups.com
Yes, it works for date but it does not work for time. I'm not sure how
this would look implemented into web2py. (You should read the page more
thoroughly.)

-Tim
--
Timothy Farrell <tfar...@swgen.com>

achipa

unread,
Jan 15, 2009, 6:25:06 AM1/15/09
to web2py Web Framework
I believe json doesn't do this by default for at least a couple of
reasons

a) the solution would be programming language/version/application
specific
b) you can't differentiate strings from dates as json has no metadata
on it's fields
c) you'd have to stick to a RFC style complicated date parser to avoid
regional (=date format) problems

I actually made a patch for this myself, but it's much more cludgy
(that's why I didn't post it here), in my solution made the field look
like TIMESTAMP#TZ and added a #DATE to the key name (since I load it,
too, a hook removes this and recreates the date object transparently).

Timothy Farrell

unread,
Jan 15, 2009, 8:57:18 AM1/15/09
to web...@googlegroups.com
Did you look at my implementation?

1) Yes, this is a problem, but we can't get around it when we still need
a solution. My solution works for Python 2.5 and 2.6 and the dates are
supported in all major, modern browsers.
2) My implementation doesn't use strings in a JSON object. It relies on
the JSON parser parsing the JSON as a Javascript engine would.
3) My implementation doesn't parse a timestamp string, rather it builds
a Javascript date object using the date. That should be a little more
cross-locale (is that the right term?)

-tim
--
Timothy Farrell <tfar...@swgen.com>

achipa

unread,
Jan 15, 2009, 10:10:00 AM1/15/09
to web2py Web Framework
Yes, I've taken a brief look

1) agreed
2) I got the impression the 'new Date..' is stored as string in the
JSON object, and thus it would be indistinguishable from a regular
string field with the same content, but if it's just for eval-ing, it
works (although I always get goosebumps when I see code in JSON, the
next iteration of ecma is introducing parsejson() instead of eval()
for exactly this reason). I guess it's about choice.
3) it's ok, as long as you don't use/care about TZ

Timothy Farrell

unread,
Jan 15, 2009, 10:14:28 AM1/15/09
to web...@googlegroups.com
3) Good point. Currently all of our customers are (should be) in the
same timezone. That may need some adjustment in the future.
--
Timothy Farrell <tfar...@swgen.com>

carlo

unread,
Jan 16, 2009, 9:32:16 AM1/16/09
to web2py Web Framework
There is also a dates JQuery plugin which seems to be able to convert
a string date in a Date javascript object: this could be useful as you
can have Json serialize dates as strings and then convert them to
javascript Date objects. Anyone tried it?

carlo
Reply all
Reply to author
Forward
0 new messages