Ticket #4656: json.py

File json.py, 984 bytes (added by Matthew Flanagan <mattimustang@…>, 7 years ago)
Line 
1"""
2Serialize data to/from JSON
3"""
4from django.utils import simplejson
5from python import Serializer as PythonSerializer
6from django.core.serializers.json import Deserializer as JSONDeserializer, \
7    DjangoJSONEncoder
8
9class 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
32Deserializer = JSONDeserializer
Back to Top