I am trying to encode a python dictionary as JSON and make sure that the resulting string is utf-8 encoded.
In my first try my code looked like the following.
import json
from json import JSONEncoder
class MyEncoder(JSONEncoder):
def default(self, o):
return o.__dict__
draft = json.dumps(myObject, sort_keys=True, indent=2, cls=MyEncoder, ensure_ascii=False)
draft = draft.encode('utf8')
print("DEBUG: CLI will send the following JSON: {}".format(draft))
Which results in
DEBUG: CLI will send the following JSON: b'{\n "a": "ABC",\n "d": "Lorem ipsum dolor sit amet, invidunt \xc3\xbct",\n "t": "SingleApp"\n}'
I expected (Note the 'ü' and the whitespace characters):
DEBUG: CLI will send the following JSON: {
"a": "ABC",
"d": "Lorem ipsum dolor sit amet, invidunt üt",
"t": "SingleApp"
}
In my second try I left out draft = draft.encode('utf8')
and the result looks as expected but
can I trust it to always work like that? Is it going to be encoded differently on a differently configured machine?
"Works for me" doesn'nt work for me.