Code review comment for lp:~leonardr/launchpadlib/convert-datetime-309950

Revision history for this message
Edwin Grubbs (edwin-grubbs) wrote :

Hi Leonard,

This is a nice change. I just have a concern about datetime.date support, which I am assuming should be included in this branch. See below.

merge-conditional

=== added file 'launchpadlib/_utils/json.py'
--- launchpadlib/_utils/json.py 1970-01-01 00:00:00 +0000
+++ launchpadlib/_utils/json.py 2009-01-21 20:59:09 +0000
@@ -0,0 +1,35 @@
+# Copyright 2009 Canonical Ltd.
+
+# This file is part of launchpadlib.
+#
+# launchpadlib is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Lesser General Public License as
+# published by the Free Software Foundation, either version 3 of the
+# License, or (at your option) any later version.
+#
+# launchpadlib is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with launchpadlib. If not, see
+# <http://www.gnu.org/licenses/>.
+
+"""Classes for working with JSON."""
+
+__metaclass__ = type
+__all__ = ['DatetimeJSONEncoder']
+
+import datetime
+import simplejson
+
+class DatetimeJSONEncoder(simplejson.JSONEncoder):
+ """A JSON encoder that understands datetime objects.
+
+ Datetime objects are formatted according to ISO 1601.
+ """
+ def default(self, obj):
+ if isinstance(obj, datetime.datetime):
+ return obj.isoformat()
+ return simplejson.JSONEncoder.default(self, obj)

A datetime.datetime object is also an instance of the datetime.date
class, but not the other way around. You also don't have any tests
for datetime.date objects.

review: Approve

« Back to merge proposal