I have a Django app, using tasypie to serialize some data.
There is a name
"Glòria"
(with an accented 'o') in the database, but this is not being serialized correctly. In the json produced by tasypie, it comes out as
"Glòria"
The serializer class looks like this:
import json as simplejson
class PrettyJSONSerializer(Serializer):
json_indent = 2
def to_json(self, data, options=None):
options = options or {}
data = self.to_simple(data, options)
return simplejson.dumps(data, cls=json.DjangoJSONEncoder,
sort_keys=True, ensure_ascii=False, indent=self.json_indent)
Changing the attribute on the simplejson.dumps to
ensure_ascii=True
returns the following:
"Gl\u00f2ria"
stror aunicodeobject?"Gl\u00f2ria"version is actually a valid JSON representation ofGlòria. Are you sure the problem withensure_ascii=Falseis with the serializer and not the client?s="this is a Gl\u00f2ria test".decode('unicode-escape');print s,repr(s)printsthis is a Glòria test u'this is a Gl\xf2ria test'. At least, it'll print that if your console is set to use utf-8 encoding. :)