2006-06-30 00:42:49 +08:00
|
|
|
"""
|
|
|
|
Serialize data to/from JSON
|
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
2008-07-30 18:40:37 +08:00
|
|
|
from StringIO import StringIO
|
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
from django.core.serializers.python import Serializer as PythonSerializer
|
|
|
|
from django.core.serializers.python import Deserializer as PythonDeserializer
|
2008-07-18 11:47:27 +08:00
|
|
|
from django.utils import datetime_safe
|
2008-07-30 18:40:37 +08:00
|
|
|
from django.utils import simplejson
|
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
try:
|
|
|
|
import decimal
|
|
|
|
except ImportError:
|
|
|
|
from django.utils import _decimal as decimal # Python 2.3 fallback
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
class Serializer(PythonSerializer):
|
|
|
|
"""
|
|
|
|
Convert a queryset to JSON.
|
|
|
|
"""
|
2007-12-17 14:53:15 +08:00
|
|
|
internal_use_only = False
|
2008-07-18 11:47:27 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def end_serialization(self):
|
2007-06-01 21:39:08 +08:00
|
|
|
self.options.pop('stream', None)
|
|
|
|
self.options.pop('fields', None)
|
2009-12-14 20:39:20 +08:00
|
|
|
self.options.pop('use_natural_keys', None)
|
2007-05-21 09:29:58 +08:00
|
|
|
simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options)
|
2007-04-25 18:12:05 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def getvalue(self):
|
2007-04-25 18:12:05 +08:00
|
|
|
if callable(getattr(self.stream, 'getvalue', None)):
|
|
|
|
return self.stream.getvalue()
|
2006-06-30 00:42:49 +08:00
|
|
|
|
|
|
|
def Deserializer(stream_or_string, **options):
|
|
|
|
"""
|
|
|
|
Deserialize a stream or string of JSON data.
|
|
|
|
"""
|
|
|
|
if isinstance(stream_or_string, basestring):
|
|
|
|
stream = StringIO(stream_or_string)
|
|
|
|
else:
|
|
|
|
stream = stream_or_string
|
2009-12-22 23:18:51 +08:00
|
|
|
for obj in PythonDeserializer(simplejson.load(stream), **options):
|
2006-06-30 00:42:49 +08:00
|
|
|
yield obj
|
2007-04-25 18:12:05 +08:00
|
|
|
|
2007-05-21 09:29:58 +08:00
|
|
|
class DjangoJSONEncoder(simplejson.JSONEncoder):
|
2006-06-30 00:42:49 +08:00
|
|
|
"""
|
2007-05-21 09:29:58 +08:00
|
|
|
JSONEncoder subclass that knows how to encode date/time and decimal types.
|
2006-06-30 00:42:49 +08:00
|
|
|
"""
|
2007-04-25 18:12:05 +08:00
|
|
|
|
|
|
|
DATE_FORMAT = "%Y-%m-%d"
|
2006-06-30 00:42:49 +08:00
|
|
|
TIME_FORMAT = "%H:%M:%S"
|
2007-04-25 18:12:05 +08:00
|
|
|
|
2006-06-30 00:42:49 +08:00
|
|
|
def default(self, o):
|
2006-07-31 22:59:53 +08:00
|
|
|
if isinstance(o, datetime.datetime):
|
2008-07-18 11:47:27 +08:00
|
|
|
d = datetime_safe.new_datetime(o)
|
|
|
|
return d.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT))
|
2006-07-31 22:59:53 +08:00
|
|
|
elif isinstance(o, datetime.date):
|
2008-07-18 11:47:27 +08:00
|
|
|
d = datetime_safe.new_date(o)
|
|
|
|
return d.strftime(self.DATE_FORMAT)
|
2006-06-30 00:42:49 +08:00
|
|
|
elif isinstance(o, datetime.time):
|
|
|
|
return o.strftime(self.TIME_FORMAT)
|
2007-05-21 09:29:58 +08:00
|
|
|
elif isinstance(o, decimal.Decimal):
|
|
|
|
return str(o)
|
2006-06-30 00:42:49 +08:00
|
|
|
else:
|
2007-05-21 09:29:58 +08:00
|
|
|
return super(DjangoJSONEncoder, self).default(o)
|
|
|
|
|
|
|
|
# Older, deprecated class name (for backwards compatibility purposes).
|
|
|
|
DateTimeAwareJSONEncoder = DjangoJSONEncoder
|
|
|
|
|