tortoise / tortoise-orm

Familiar asyncio ORM for python, built with relations in mind
https://tortoise.github.io
Apache License 2.0
4.51k stars 369 forks source link

could you use orjson in tortoise-orm? #598

Open zedrood opened 3 years ago

zedrood commented 3 years ago

orjson is the fastest Python library for JSON, could you use orjson in tortoise-orm? based on pydantic feature it is possible to use orjson on pydantic_model_creator in from tortoise.contrib.pydantic import pydantic_model_creator and instead of rapidjson in fields.data.py the code bellow:

try:
    # Use python-rapidjson as an optional accelerator
    import rapidjson

    JSON_DUMPS = rapidjson.dumps
    JSON_LOADS = rapidjson.loads
except ImportError:  # pragma: nocoverage
    pass

use this one based on answer

import importlib
orjson_spec = importlib.util.find_spec("orjson")
if orjson_spec:
    # Use orjson as an optional accelerator
    # pip install orjson 
    import orjson

    JSON_DUMPS = orsjon.dumps
    JSON_LOADS = orsjon.loads
long2ice commented 3 years ago

You can pass encoder and decoder to JSONField