Flask JSONEncoder set ensure_ascii to False Flask JSONEncoder set ensure_ascii to False flask flask

Flask JSONEncoder set ensure_ascii to False


You are setting a class attribute when altering app.json_encoder.ensure_ascii, which is not the same thing as an instance attribute.

The JSONEncoder.__init__ method sets an instance attribute to True, as that is the default value for the ensure_ascii argument. Setting the class attribute won't prevent the __init__ method from setting an instance attribute, and it is the instance attribute that wins here.

You could just have your subclass set the argument to False:

class NonASCIIJSONEncoder(json.JSONEncoder):    def __init__(self, **kwargs):        kwargs['ensure_ascii'] = False        super(NonASCIIJSONEncoder, self).__init__(**kwargs)