david / django-portablecontacts (http://portablecontacts.net/)

Support for PortableContacts in Django.

r2:d759edee4b66 36 loc 1.2 KB embed / history / annotate / raw /
from django.utils import simplejson
from django.core.serializers.json import DjangoJSONEncoder

from portablecontacts.serializers.python import Serializer as PythonSerializer

class Serializer(PythonSerializer):
    """
    Convert a queryset to JSON.
    """
    internal_use_only = False

    def end_serialization(self):
        """
        End serialization -- end the document.
        """
        objects = {}
        if not self.options.pop('unique', False):
            objects.update({
                'startIndex': self.options.pop('startIndex', 0),
                'totalResults': self.options.pop('totalResults', 0),
            })
        if self.options.get('count', False):
            objects.update({
                'itemsPerPage': self.options.pop('count'),
            })
        self.options.pop('stream', None)
        self.options.pop('fields', None)
        objects.update({
            'entry': self.options.pop('for_self', False) and self.objects[0] or self.objects
        })
        simplejson.dump(objects, self.stream, cls=DjangoJSONEncoder, **self.options)

    def getvalue(self):
        if callable(getattr(self.stream, 'getvalue', None)):
            return self.stream.getvalue()