How to prevent json response from encoding escape sequence?

How to make response decode dict in UTF-8?

from sanic import Sanic
from sanic.response import json as s_json
import json

app = Sanic("App Name")

d = {"aa": "тестовое наименование"}

async def test(request):
    return s_json(d)

if __name__ == "__main__":"", port=8000)

In web browser I am getting:

{"aa":"\u0442\u0435\u0441\u0442\u043e\u0432\u043e\u0435 \u043d\u0430\u0438\u043c\u0435\u043d\u043e\u0432\u0430\u043d\u0438\u0435"}

I googled, but I did not found any answer.

Hi @bubnenkoff

Thanks for your question. This is an easy one.

The Sanic response.json() helper takes any **kwargs you give it and passes it onto the ujons.dumps() function. ujson (and python’s built-in JSON library) has an option called ensure_ascii that ascii-encodes the output by default, that is what you’re seeing here.
Luckily it is easy to disable that option by passing ensure_ascii=False to dumps(), which means you can pass that option to response.json() too, and it will be pass onto ujson. Eg:

async def test(request):
    return s_json(d, ensure_ascii=False)

For that matter, it is also worth noting that you can pass your own dumps function for serializing. There are many in the python ecosystem.

from orjson import dumps

async def test(request):
    return s_json(d, dumps=dumps)