2006-06-30 00:42:49 +08:00
|
|
|
"""
|
|
|
|
Serialize data to/from JSON
|
|
|
|
"""
|
|
|
|
|
|
|
|
import datetime
|
2010-05-04 22:00:30 +08:00
|
|
|
import decimal
|
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-30 18:40:37 +08:00
|
|
|
from django.utils import simplejson
|
2011-11-18 21:01:06 +08:00
|
|
|
from django.utils.timezone import is_aware
|
2008-07-30 18:40:37 +08:00
|
|
|
|
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):
|
2011-12-18 07:00:41 +08:00
|
|
|
if simplejson.__version__.split('.') >= ['2', '1', '3']:
|
|
|
|
# Use JS strings to represent Python Decimal instances (ticket #16850)
|
|
|
|
self.options.update({'use_decimal': False})
|
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
|
|
|
"""
|
|
|
|
def default(self, o):
|
2011-11-18 21:01:06 +08:00
|
|
|
# See "Date Time String Format" in the ECMA-262 specification.
|
2006-07-31 22:59:53 +08:00
|
|
|
if isinstance(o, datetime.datetime):
|
2011-11-18 21:01:06 +08:00
|
|
|
r = o.isoformat()
|
|
|
|
if o.microsecond:
|
|
|
|
r = r[:23] + r[26:]
|
|
|
|
if r.endswith('+00:00'):
|
|
|
|
r = r[:-6] + 'Z'
|
|
|
|
return r
|
2006-07-31 22:59:53 +08:00
|
|
|
elif isinstance(o, datetime.date):
|
2011-11-18 21:01:06 +08:00
|
|
|
return o.isoformat()
|
2006-06-30 00:42:49 +08:00
|
|
|
elif isinstance(o, datetime.time):
|
2011-11-18 21:01:06 +08:00
|
|
|
if is_aware(o):
|
|
|
|
raise ValueError("JSON can't represent timezone-aware times.")
|
|
|
|
r = o.isoformat()
|
|
|
|
if o.microsecond:
|
|
|
|
r = r[:12]
|
|
|
|
return r
|
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
|
|
|
|
|