Google Groups Home
Help | Sign in
json serialization
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  9 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
siniy  
View profile  
 More options Jul 31 2006, 4:50 am
From: "siniy" <msi...@gmail.com>
Date: Mon, 31 Jul 2006 08:50:17 -0000
Local: Mon, Jul 31 2006 4:50 am
Subject: json serialization
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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jyrki Pulliainen  
View profile  
 More options Jul 31 2006, 5:14 am
From: "Jyrki Pulliainen" <jyrki.pulliai...@gmail.com>
Date: Mon, 31 Jul 2006 12:14:28 +0300
Local: Mon, Jul 31 2006 5:14 am
Subject: Re: json serialization
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.pulliai...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
limodou  
View profile  
 More options Jul 31 2006, 5:18 am
From: limodou <limo...@gmail.com>
Date: Mon, 31 Jul 2006 17:18:46 +0800
Local: Mon, Jul 31 2006 5:18 am
Subject: Re: json serialization
On 7/31/06, Jyrki Pulliainen <jyrki.pulliai...@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

    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gábor Farkas  
View profile  
 More options Jul 31 2006, 9:19 am
From: Gábor Farkas <ga...@nekomancer.net>
Date: Mon, 31 Jul 2006 15:19:02 +0200
Local: Mon, Jul 31 2006 9:19 am
Subject: Re: json serialization

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jyrki Pulliainen  
View profile  
 More options Jul 31 2006, 9:38 am
From: "Jyrki Pulliainen" <jyrki.pulliai...@gmail.com>
Date: Mon, 31 Jul 2006 16:38:23 +0300
Local: Mon, Jul 31 2006 9:38 am
Subject: Re: json serialization
2006/7/31, Gábor Farkas <ga...@nekomancer.net>:

Definetly not

..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.pulliai...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jyrki Pulliainen  
View profile  
 More options Jul 31 2006, 9:54 am
From: "Jyrki Pulliainen" <jyrki.pulliai...@gmail.com>
Date: Mon, 31 Jul 2006 16:54:09 +0300
Local: Mon, Jul 31 2006 9:54 am
Subject: Re: json serialization
I filed a ticket for this, see http://code.djangoproject.com/ticket/2460

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Gábor Farkas  
View profile  
 More options Jul 31 2006, 9:57 am
From: Gábor Farkas <ga...@nekomancer.net>
Date: Mon, 31 Jul 2006 15:57:54 +0200
Local: Mon, Jul 31 2006 9:57 am
Subject: Re: json serialization

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jyrki Pulliainen  
View profile  
 More options Aug 1 2006, 5:11 am
From: "Jyrki Pulliainen" <jyrki.pulliai...@gmail.com>
Date: Tue, 1 Aug 2006 12:11:26 +0300
Local: Tues, Aug 1 2006 5:11 am
Subject: Re: json serialization
2006/7/31, Gábor Farkas <ga...@nekomancer.net>:

> 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"

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

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


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Jyrki Pulliainen  
View profile  
 More options Aug 1 2006, 5:13 am
From: "Jyrki Pulliainen" <jyrki.pulliai...@gmail.com>
Date: Tue, 1 Aug 2006 12:13:16 +0300
Local: Tues, Aug 1 2006 5:13 am
Subject: Re: json serialization
2006/7/31, Jyrki Pulliainen <jyrki.pulliai...@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.pulliai...@gmail.com


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google