Mark Huang <[email protected]> writes:

> Could you post the JSON renderer to deal with datetimes as well?

Sure, here it is::

    class Encoder(JSONEncoder):
        """Extends JSONEncoder for date and decimal types."""

        def push_date(self, d):
            """Serialize the given datetime.date object to a JSON string."""
            # Default is ISO 8601 compatible (standard notation).
            return "%04d-%02d-%02d" % (d.year, d.month, d.day)

        def push_timedelta(self, t):
            """Serialize the given datetime.timedelta object to a JSON 
string."""
            days = t.days
            if days < 0:
                minus = "-"
                days = -days - 1
                seconds = 24*60*60 - t.seconds
            else:
                minus = ""
                seconds = t.seconds
            secs = seconds % 60
            seconds /= 60
            mins = seconds % 60
            hours = seconds / 60
            return "%s%d:%02d:%02d:%02d" % (minus, days, hours, mins, secs)

        def push_time(self, t):
            """Serialize the given datetime.time object to a JSON string."""
            # Default is ISO 8601 compatible (standard notation).
            return "%02d:%02d:%02d" % (t.hour, t.minute, t.second)

        def push_datetime(self, dt):
            """Serialize the given datetime.datetime object to a JSON string."""
            # Default is ISO 8601 compatible (standard notation).
            # Don't use strftime because that can't handle dates before 1900.
            return ("%04d-%02d-%02dT%02d:%02d:%02d" %
                    (dt.year, dt.month, dt.day, dt.hour, dt.minute, dt.second))

        def default(self, o):
            # We MUST check for a datetime.datetime instance before 
datetime.date.
            # datetime.datetime is a subclass of datetime.date, and therefore
            # instances of it are also instances of datetime.date.
            if isinstance(o, datetime.datetime):
                return self.push_datetime(o)
            elif isinstance(o, datetime.date):
                return self.push_date(o)
            elif isinstance(o, datetime.timedelta):
                return self.push_timedelta(o)
            elif isinstance(o, datetime.time):
                return self.push_time(o)
            elif isinstance(o, decimal.Decimal):
                return str(o)
            else:
                return JSONEncoder.default(self, o)

hth,
ciao, lele.
-- 
nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri
real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia.
[email protected]  |                 -- Fortunato Depero, 1929.

-- 
You received this message because you are subscribed to the Google Groups 
"pylons-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/pylons-discuss?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to