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"
I can't comment (yet..) so I'm posting a reply. Python 2 isn't exactly fun with encodings.
Glòria is the correct utf-8 encoded representation of the data in bytes. Gl\u00f2ria is Python 2 internal representation of unicode strings. json.dumps returns a python unicode string. What you probably want to do is encode the output of json.dumps in utf8.
import json
data = u'Gl\xf2ria'
encoded_data = json.dumps(s, ensure_ascii=False).encode("utf8")
print(encoded_data)
prints Glòria.
Edit: Just to make sure
Glòria = Gl\xc3\xb2ria. Printed with the print statement both should display correctly as Glòria.