Login

utf8-friendly dumpdata management command (no escape symbols) #2

Author:
kmike
Posted:
March 19, 2011
Language:
Python
Version:
1.2
Tags:
fixtures management dumpdata
Score:
5 (after 5 ratings)

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

  1. utf8-friendly dumpdata management command (no escape symbols) #3 by inductor 1 year, 11 months ago
  2. utf8-friendly dumpdata management command for yaml (no escape symbols) by mucius 1 year, 11 months ago
  3. Easier custom Model Manager Chaining by gsakkis 4 years, 9 months ago
  4. streaming dump_data by kcarnold 6 years ago
  5. streaming serializer by kcarnold 6 years ago

Comments

Please login first before commenting.