simplejson and date/datetime help

481 views
Skip to first unread message

mdipierro

unread,
May 22, 2009, 3:35:31 PM5/22/09
to web2py Web Framework
can somebody figure out how to serialize in isoformat date and
datetimes in simplejson?

I would include it in the new gluon/serialize.py/json

Massimo

mdipierro

unread,
May 23, 2009, 1:57:59 AM5/23/09
to web2py Web Framework
forget it. I did it.

Massimo

weheh

unread,
May 24, 2009, 4:28:42 PM5/24/09
to web2py Web Framework
Massimo, are you saying that simplejson now supports datetime? If yes,
hallelujah! It's been a real pain converting all my datetimes in my
calendar program to strings, so this would simplify my code
considerably.
> > Massimo- Hide quoted text -
>
> - Show quoted text -

mdipierro

unread,
May 24, 2009, 7:27:45 PM5/24/09
to web2py Web Framework
yes it does in the sense that you can serialize date, datetime and
time objects using web2py's simplejson but they are serialized into
isostrings, not javascript date objects. Should they be serialized
into javascript Date objects? pros? Cons?

Massimo

Iceberg

unread,
May 24, 2009, 9:35:54 PM5/24/09
to web2py Web Framework
IMHO, the default JSONEncoder need not be serialized into javascript
Date objects, otherwise it is troublesome when using in non-js
situation. But we can have another subclass, say JSONEncoderForJs, for
that purpose. See also the docstring of JSONEncoder:

To extend this to recognize other objects, subclass and implement
a
``.default()`` method with another method that returns a
serializable
object for ``o`` if possible, otherwise it should call the
superclass
implementation (to raise ``TypeError``).

weheh

unread,
Jun 1, 2009, 2:59:12 PM6/1/09
to web2py Web Framework
I tried it with 1.6.2.3 and it didn't work: TypeError:
datetime.datetime(2009, 6, 1, 0, 0) is not JSON serializable

On May 24, 7:27 pm, mdipierro <mdipie...@cs.depaul.edu> wrote:
> yes it does in the sense that you can serialize date,datetimeand
> time objects using web2py's simplejson but they are serialized into
> isostrings, not javascript date objects. Should they be serialized
> into javascript Date objects? pros? Cons?
>
> Massimo
>
> On May 24, 3:28 pm, weheh <richard_gor...@verizon.net> wrote:
>
> > Massimo, are you saying that simplejson now supportsdatetime? If yes,

mdipierro

unread,
Jun 1, 2009, 5:35:06 PM6/1/09
to web2py Web Framework
Look for this in gluon/contrib/simplejson/encoder.py

if isinstance(o, (datetime.date,
datetime.datetime,
datetime.time)):
return o.isoformat()[:19].replace('T',' ')
raise TypeError("%r is not JSON serializable" % (o,))


If it is not there something is wrong with your installation. Try
download it again.

weheh

unread,
Jun 14, 2009, 9:19:13 AM6/14/09
to web2py Web Framework
Sorry for the dumb question, but what's the path to qluon? I can't
find it. I just downloaded the latest version of web2py and json still
chokes on datetime.

mdipierro

unread,
Jun 14, 2009, 4:58:24 PM6/14/09
to web2py Web Framework
do

import gluon.contrib.simplejson as simplejson

not

import simplejson

weheh

unread,
Jun 14, 2009, 9:32:28 PM6/14/09
to web2py Web Framework
I'm already doing
import gluon.contrib.simplejson as sj
and then
return sj.dump(x)

mdipierro

unread,
Jun 15, 2009, 1:51:29 AM6/15/09
to web2py Web Framework
Then your problem is that somehow you did not upgrade the code in the
contrib folder.
Other people have reported problems in upgrading from svn.

Massimo
Reply all
Reply to author
Forward
0 new messages