Flask: orjson instead of json module for decoding

Flask: orjson instead of json module for decoding

我正在使用 Flask 并且有很多请求。 flask 使用的 json 模块非常慢。我自动可以使用 simplejson,但那有点慢,而不是更快。根据文档我可以定义一个解码器(flask.json_decoder), but orjson doesn't have this class. I only have the function loads and dumps. Can somebody explain me, how I can exchange the json module with orjson?最后我只想使用加载和转储功能,但我无法连接我的尾巴。

一个非常基本的实现可能如下所示:

class ORJSONDecoder:

    def __init__(self, **kwargs):
        # eventually take into consideration when deserializing
        self.options = kwargs

    def decode(self, obj):
        return orjson.loads(obj)


class ORJSONEncoder:

    def __init__(self, **kwargs):
        # eventually take into consideration when serializing
        self.options = kwargs

    def encode(self, obj):
        # decode back to str, as orjson returns bytes
        return orjson.dumps(obj).decode('utf-8')


app = Flask(__name__)
app.json_encoder = ORJSONEncoder
app.json_decoder = ORJSONDecoder