Django RestFramework JSON Renderer Backed by orjson
Low maintenance activity. Last release 11 months ago, commits in 0 of the last 26 weeks. Weaker on commit activity and release cadence.
pip install drf-orjson-rendererDjango Rest Framework ORJSON Renderer
drf_orjson_renderer is JSON renderer and parser for Django Rest Framework
using the orjson library. Backed by
Rust, orjson is safe, correct and fast. ⚡️
In addition, unlike some performance optimized DRF renderers, It also renders pretty printed JSON when requests are made via RestFramework's BrowsableAPI.
You get:
- The safety of Rust
- The speed of orjson when requests are made with
Accept: appliation/jsonHTTP header or when requests are made with an unspecifiedAcceptheader. - The convenience of formatted output when requested with
Accept: text/html. - The ability to pass your own
defaultfunction definition.
Installation
pip install drf_orjson_renderer
You can then set the ORJSONRenderer class as your default renderer in your settings.py
REST_FRAMEWORK = {
"DEFAULT_RENDERER_CLASSES": (
"drf_orjson_renderer.renderers.ORJSONRenderer",
"rest_framework.renderers.BrowsableAPIRenderer",
),
}
To modify how data is serialized, specify options in your settings.py
REST_FRAMEWORK = {
"ORJSON_RENDERER_OPTIONS": (
orjson.OPT_NON_STR_KEYS,
orjson.OPT_SERIALIZE_DATACLASS,
orjson.OPT_SERIALIZE_NUMPY,
),
}
Also you can set the ORJSONParser class as your default parser in your settings.py
REST_FRAMEWORK = {
"DEFAULT_PARSER_CLASSES": (
"drf_orjson_renderer.parsers.ORJSONParser",
),
}
Passing Your Own default Function
By default, the ORJSONRenderer will pass a default function as a helper for
serializing objects that orjson doesn't recognize. That should cover the most
common cases found in a Django web application. If you find you have an object
it doesn't recognize you can pass your own default function by overriding the
method of your view: