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

git-svn-id: http://code.djangoproject.com/svn/django/trunk@5075 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Malcolm Tredinnick 2007-04-25 10:12:05 +00:00
parent 74bab89178
commit e23ababb52
2 changed files with 11 additions and 8 deletions

View File

@ -105,9 +105,11 @@ class Serializer(object):
def getvalue(self): 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): class Deserializer(object):
""" """

View File

@ -17,9 +17,10 @@ class Serializer(PythonSerializer):
""" """
def end_serialization(self): def end_serialization(self):
simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **self.options) simplejson.dump(self.objects, self.stream, cls=DateTimeAwareJSONEncoder, **self.options)
def getvalue(self): def getvalue(self):
return self.stream.getvalue() if callable(getattr(self.stream, 'getvalue', None)):
return self.stream.getvalue()
def Deserializer(stream_or_string, **options): def Deserializer(stream_or_string, **options):
""" """
@ -31,15 +32,15 @@ def Deserializer(stream_or_string, **options):
stream = stream_or_string stream = stream_or_string
for obj in PythonDeserializer(simplejson.load(stream)): for obj in PythonDeserializer(simplejson.load(stream)):
yield obj yield obj
class DateTimeAwareJSONEncoder(simplejson.JSONEncoder): class DateTimeAwareJSONEncoder(simplejson.JSONEncoder):
""" """
JSONEncoder subclass that knows how to encode date/time types 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" TIME_FORMAT = "%H:%M:%S"
def default(self, o): def default(self, o):
if isinstance(o, datetime.datetime): if isinstance(o, datetime.datetime):
return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT)) return o.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))