This is a version of http://djangosnippets.org/snippets/2258/ that should work with special chars (e.g. quotes) in json data.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23  | # serializers/json_pretty.py
"""
Add the line to settings.py::
    SERIALIZATION_MODULES = {'json-pretty': 'serializers.json_pretty'}
And call dumpdata as follows::
    ./manage.py dumpdata --format=json-pretty <app_name>
"""
import codecs
from django.utils import simplejson
from django.core.serializers.json import Serializer as JSONSerializer
from django.core.serializers.json import Deserializer
from django.core.serializers.json import DjangoJSONEncoder
class Serializer(JSONSerializer):
    def end_serialization(self):
        stream = codecs.getwriter('utf8')(self.stream)
        simplejson.dump(self.objects, stream, cls=DjangoJSONEncoder,
                        ensure_ascii=False, **self.options)
 | 
More like this
- Add Toggle Switch Widget to Django Forms by OgliariNatan 1 month, 4 weeks ago
 - get_object_or_none by azwdevops 5 months, 2 weeks ago
 - Mask sensitive data from logger by agusmakmun 7 months, 2 weeks ago
 - Template tag - list punctuation for a list of items by shapiromatron 1 year, 9 months ago
 - JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 1 year, 9 months ago
 
Comments
Please login first before commenting.