json serialization

32 views
Skip to first unread message

siniy

unread,
Jul 31, 2006, 4:50:17 AM7/31/06
to Django users
Hi all,
I've downloaded today a new release of Django and played with json
serialization. I found that if you use DateTime field the resulting
json string contains only date, but not all datetime. So I viewed a
source code of django/core/serializers/json.py and found that:

def default(self, o):
if isinstance(o, datetime.date):
return o.strftime(self.DATE_FORMAT)
elif isinstance(o, datetime.time):
return o.strftime(self.TIME_FORMAT)
elif isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))
....

I know that isinstance(o, datetime.date) returns "True" even "o" is a
datetime object. But I don't know - may be it's a python bug? My python
version 2.4.3 from Ubuntu Dapper. So I replace parts of django code and
all works as I want:

def default(self, o):
if isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))
elif isinstance(o, datetime.time):
return o.strftime(self.TIME_FORMAT)
elif isinstance(o, datetime.date):
return o.strftime(self.DATE_FORMAT)
else:
return super(self, DateTimeAwareJSONEncoder).defaul

Jyrki Pulliainen

unread,
Jul 31, 2006, 5:14:28 AM7/31/06
to django...@googlegroups.com
2006/7/31, siniy <msi...@gmail.com>:

> I know that isinstance(o, datetime.date) returns "True" even "o" is a
> datetime object. But I don't know - may be it's a python bug? My python
> version 2.4.3 from Ubuntu Dapper.


I can confirm this behaviour with Debian Testing's Python 2.3.5 too.

--
Jyrki // jyrki.pu...@gmail.com

limodou

unread,
Jul 31, 2006, 5:18:46 AM7/31/06
to django...@googlegroups.com
On 7/31/06, Jyrki Pulliainen <jyrki.pu...@gmail.com> wrote:
>
> 2006/7/31, siniy <msi...@gmail.com>:
> > I know that isinstance(o, datetime.date) returns "True" even "o" is a
> > datetime object. But I don't know - may be it's a python bug? My python
> > version 2.4.3 from Ubuntu Dapper.

I think you should make it a ticket and upload your patch.


>
>
> I can confirm this behaviour with Debian Testing's Python 2.3.5 too.
>


--
I like python!
My Blog: http://www.donews.net/limodou
My Django Site: http://www.djangocn.org
NewEdit Maillist: http://groups.google.com/group/NewEdit

Gábor Farkas

unread,
Jul 31, 2006, 9:19:02 AM7/31/06
to django...@googlegroups.com
siniy wrote:
> Hi all,
> I've downloaded today a new release of Django and played with json
> serialization. I found that if you use DateTime field the resulting
> json string contains only date, but not all datetime. So I viewed a
> source code of django/core/serializers/json.py and found that:
>
> def default(self, o):
> if isinstance(o, datetime.date):
> return o.strftime(self.DATE_FORMAT)
> elif isinstance(o, datetime.time):
> return o.strftime(self.TIME_FORMAT)
> elif isinstance(o, datetime.datetime):
> return o.strftime("%s %s" % (self.DATE_FORMAT,
> self.TIME_FORMAT))
> ....
>
> I know that isinstance(o, datetime.date) returns "True" even "o" is a
> datetime object. But I don't know - may be it's a python bug? My python
> version 2.4.3 from Ubuntu Dapper. So I replace parts of django code and
> all works as I want:
>

no, it's not a bug.

datetime.datetime inherits from datetime.

you can check it like this:

>>> datetime.datetime.__mro__
(<type 'datetime.datetime'>, <type 'datetime.date'>, <type 'object'>)
>>>

(mro is method-resolution-order)

as you see 'datetime' inherits from 'date', which inherits from 'object'.

and if you check the python-help, you'll see that
isinstance "Return whether an object is an instance of a class or of a
subclass thereof".

and because datetime is a subclass of date, isinstance returns True.


maybe you could try this:

if type(o) == type(datetime.datetime):
// your code here


gabor

Jyrki Pulliainen

unread,
Jul 31, 2006, 9:38:23 AM7/31/06
to django...@googlegroups.com
2006/7/31, Gábor Farkas <ga...@nekomancer.net>:

>
> siniy wrote:
> > Hi all,
> > I've downloaded today a new release of Django and played with json
> > serialization. I found that if you use DateTime field the resulting
> > json string contains only date, but not all datetime. So I viewed a
> > source code of django/core/serializers/json.py and found that:
> >
> > def default(self, o):
> > if isinstance(o, datetime.date):
> > return o.strftime(self.DATE_FORMAT)
> > elif isinstance(o, datetime.time):
> > return o.strftime(self.TIME_FORMAT)
> > elif isinstance(o, datetime.datetime):
> > return o.strftime("%s %s" % (self.DATE_FORMAT,
> > self.TIME_FORMAT))
> > ....
> >
> > I know that isinstance(o, datetime.date) returns "True" even "o" is a
> > datetime object. But I don't know - may be it's a python bug? My python
> > version 2.4.3 from Ubuntu Dapper. So I replace parts of django code and
> > all works as I want:
> >
>
> no, it's not a bug.
>
> datetime.datetime inherits from datetime.
>

Definetly not

> you can check it like this:
>
> >>> datetime.datetime.__mro__
> (<type 'datetime.datetime'>, <type 'datetime.date'>, <type 'object'>)
> >>>
>
> (mro is method-resolution-order)
>
> as you see 'datetime' inherits from 'date', which inherits from 'object'.
>
> and if you check the python-help, you'll see that
> isinstance "Return whether an object is an instance of a class or of a
> subclass thereof".
>
> and because datetime is a subclass of date, isinstance returns True.
>
>
> maybe you could try this:
>
> if type(o) == type(datetime.datetime):
> // your code here
>

..but the real problem propably isn't that, it's the built in method
used with json. Because datetime.datetime is a subclass of
datetime.date, it never gets to the part

elif isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT,
self.TIME_FORMAT))

instead it gets caught on the very first if. That's why
datetime.datetime comparing propably should be before datetime.date
comparing

--
Jyrki // jyrki.pu...@gmail.com

Jyrki Pulliainen

unread,
Jul 31, 2006, 9:54:09 AM7/31/06
to django...@googlegroups.com
I filed a ticket for this, see http://code.djangoproject.com/ticket/2460

--
Jyrki // jyrki.pu...@gmail.com

Gábor Farkas

unread,
Jul 31, 2006, 9:57:54 AM7/31/06
to django...@googlegroups.com
Jyrki Pulliainen wrote:
> 2006/7/31, Gábor Farkas <ga...@nekomancer.net>:

>>
>> datetime.datetime inherits from datetime.
>>
>
> Definetly not
>


sorry, of course i meant

"datetime.datetime inherits from datetime.time"

gabor

Jyrki Pulliainen

unread,
Aug 1, 2006, 5:11:26 AM8/1/06
to django...@googlegroups.com
2006/7/31, Gábor Farkas <ga...@nekomancer.net>:

My bad too, since I meant it's definetly not a bug in python ;)

--
Jyrki // jyrki.pu...@gmail.com

Jyrki Pulliainen

unread,
Aug 1, 2006, 5:13:16 AM8/1/06
to django...@googlegroups.com
2006/7/31, Jyrki Pulliainen <jyrki.pu...@gmail.com>:

> I filed a ticket for this, see http://code.djangoproject.com/ticket/2460

Json serialization is now fixed in SVN Trunk version

--
Jyrki // jyrki.pu...@gmail.com

Reply all
Reply to author
Forward
0 new messages