1 | """
|
---|
2 | Serialize data to/from JSON
|
---|
3 | """
|
---|
4 | from django.utils import simplejson
|
---|
5 | from python import Serializer as PythonSerializer
|
---|
6 | from django.core.serializers.json import Deserializer as JSONDeserializer, \
|
---|
7 | DjangoJSONEncoder
|
---|
8 |
|
---|
9 | class Serializer(PythonSerializer):
|
---|
10 | """
|
---|
11 | Convert a queryset to JSON.
|
---|
12 | """
|
---|
13 | def end_serialization(self):
|
---|
14 | """Output a JSON encoded queryset."""
|
---|
15 | self.options.pop('stream', None)
|
---|
16 | self.options.pop('fields', None)
|
---|
17 | self.options.pop('excludes', None)
|
---|
18 | self.options.pop('relations', None)
|
---|
19 | self.options.pop('extras', None)
|
---|
20 | simplejson.dump(self.objects, self.stream, cls=DjangoJSONEncoder,
|
---|
21 | **self.options)
|
---|
22 |
|
---|
23 | def getvalue(self):
|
---|
24 | """
|
---|
25 | Return the fully serialized queryset (or None if the output stream
|
---|
26 | is not seekable).
|
---|
27 | """
|
---|
28 |
|
---|
29 | if callable(getattr(self.stream, 'getvalue', None)):
|
---|
30 | return self.stream.getvalue()
|
---|
31 |
|
---|
32 | Deserializer = JSONDeserializer
|
---|