Permalink
Browse files

Fixed #3435 -- Fixed serializing to a file stream. Patch from SmileyC…

…hris.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5075 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 74bab89 commit e23ababb523a006dfb7ade8b304397ac5a4537f2 @malcolmt malcolmt committed Apr 25, 2007
Showing with 11 additions and 8 deletions.
  1. +4 −2 django/core/serializers/base.py
  2. +7 −6 django/core/serializers/json.py
View
6 django/core/serializers/base.py
@@ -105,9 +105,11 @@ def handle_m2m_field(self, obj, field):
def getvalue(self):
"""
- Return the fully serialized queryset.
+ Return the fully serialized queryset (or None if the output stream is
+ not seekable).
"""
- return self.stream.getvalue()
+ if callable(getattr(self.stream, 'getvalue', None)):
+ return self.stream.getvalue()
class Deserializer(object):
"""
View
13 django/core/serializers/json.py
@@ -17,9 +17,10 @@ class Serializer(PythonSerializer):
"""
def end_serialization(self):
simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **self.options)
-
+
def getvalue(self):
- return self.stream.getvalue()
+ if callable(getattr(self.stream, 'getvalue', None)):
+ return self.stream.getvalue()
def Deserializer(stream_or_string, **options):
"""
@@ -31,15 +32,15 @@ def Deserializer(stream_or_string, **options):
stream = stream_or_string
for obj in PythonDeserializer(simplejson.load(stream)):
yield obj
-
+
class DateTimeAwareJSONEncoder(simplejson.JSONEncoder):
"""
JSONEncoder subclass that knows how to encode date/time types
"""
-
- DATE_FORMAT = "%Y-%m-%d"
+
+ DATE_FORMAT = "%Y-%m-%d"
TIME_FORMAT = "%H:%M:%S"
-
+
def default(self, o):
if isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))

0 comments on commit e23abab

Please sign in to comment.