Add PydanticJSONResponse by eltoder · Pull Request #14299 · fastapi/fastapi

YuriiMotov

@eltoder

When using this response class, json serialization is done using
Pydantic's built-in json serialization (`dump_json(r)`) instead of
generating an intermediate dict that is later serialized using a json
library (`json.dumps(dump_python(r))`).

In my testing this is 3-4x faster than using the standard json library
(the default) and 50% faster than using orjson, without requiring any
extra dependencies. This also allows configuring serialization behavior
per model using Pydantic's model_config.

@YuriiMotov

@YuriiMotov