flask-orjson

A Flask/Quart JSONProvider using the fast orjson library. Using this provider will significantly speed up reading JSON data in requests and generating JSON responses.

Usage

from flask import Flask
from flask_orjson import OrjsonProvider

app = Flask(__name__)
json_provider = OrjsonProvider(app)
app.json = json_provider

Dump Options

Orjson takes a number of options to control the accepted input as well as the format of the output. Available flags are listed in the orjson docs. Multiple flags are combined using | bitwise or.

By default, flask-orjson enables the NAIVE_UTC flag to treat datetime objects without a timezone (naive) as being in UTC.

Supported Types

Orjson has built-in support for serializing non-JSON types including datetime, date, dataclass(), and UUID. Flask-orjson adds support for Decimal and classes with an __html__ method.

One difference from the default provider is datetime and date are serialized using ISO 8601 format instead of RFC 822. This is typically what libraries and users expect nowadays.

You can use the option and default attributes to modify how non-JSON types are serialized.

Complex Data

It’s possible to write a default function to handle any data. However, we recommend using a dedicated object serialization library to first convert complex data to JSON types, before serializing that to JSON. This gives you full control over how your data is represented, as well as the ability to deserialize data in requests. Some such libraries include cattrs, Marshmallow, and Pydantic.