|
1 """ |
|
2 Serialize data to/from JSON |
|
3 """ |
|
4 |
|
5 import datetime |
|
6 import decimal |
|
7 from StringIO import StringIO |
|
8 |
|
9 from django.core.serializers.python import Serializer as PythonSerializer |
|
10 from django.core.serializers.python import Deserializer as PythonDeserializer |
|
11 from django.utils import datetime_safe |
|
12 from django.utils import simplejson |
|
13 |
|
14 class Serializer(PythonSerializer): |
|
15 """ |
|
16 Convert a queryset to JSON. |
|
17 """ |
|
18 internal_use_only = False |
|
19 |
|
20 def end_serialization(self): |
|
21 self.options.pop('stream', None) |
|
22 self.options.pop('fields', None) |
|
23 self.options.pop('use_natural_keys', None) |
|
24 simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder, **self.options) |
|
25 |
|
26 def getvalue(self): |
|
27 if callable(getattr(self.stream, 'getvalue', None)): |
|
28 return self.stream.getvalue() |
|
29 |
|
30 def Deserializer(stream_or_string, **options): |
|
31 """ |
|
32 Deserialize a stream or string of JSON data. |
|
33 """ |
|
34 if isinstance(stream_or_string, basestring): |
|
35 stream = StringIO(stream_or_string) |
|
36 else: |
|
37 stream = stream_or_string |
|
38 for obj in PythonDeserializer(simplejson.load(stream), **options): |
|
39 yield obj |
|
40 |
|
41 class DjangoJSONEncoder(simplejson.JSONEncoder): |
|
42 """ |
|
43 JSONEncoder subclass that knows how to encode date/time and decimal types. |
|
44 """ |
|
45 |
|
46 DATE_FORMAT = "%Y-%m-%d" |
|
47 TIME_FORMAT = "%H:%M:%S" |
|
48 |
|
49 def default(self, o): |
|
50 if isinstance(o, datetime.datetime): |
|
51 d = datetime_safe.new_datetime(o) |
|
52 return d.strftime("%s %s" % (self.DATE_FORMAT, self.TIME_FORMAT)) |
|
53 elif isinstance(o, datetime.date): |
|
54 d = datetime_safe.new_date(o) |
|
55 return d.strftime(self.DATE_FORMAT) |
|
56 elif isinstance(o, datetime.time): |
|
57 return o.strftime(self.TIME_FORMAT) |
|
58 elif isinstance(o, decimal.Decimal): |
|
59 return str(o) |
|
60 else: |
|
61 return super(DjangoJSONEncoder, self).default(o) |
|
62 |
|
63 # Older, deprecated class name (for backwards compatibility purposes). |
|
64 DateTimeAwareJSONEncoder = DjangoJSONEncoder |
|
65 |