Numpy Serialization

Purpose

Numpy Serialization addresses the challenge of efficiently converting numpy data types—such as arrays and scalars—into JSON format within the orjson library. Standard JSON serializers do not natively understand numpy objects, which can lead to errors or inefficient fallback serialization. This subtopic provides specialized support to recognize and serialize numpy types directly, improving both compatibility and performance when working with scientific and numerical Python codebases.

Functionality

The core feature revolves around an optional serialization flag that enables orjson to detect numpy objects during serialization and convert them into JSON-compatible representations seamlessly.

Key workflows include:

The following snippet from [bench/run_default](/projects/287/67673) illustrates usage:

from orjson import dumps, OPT_SERIALIZE_NUMPY

def default(_):
    return None  # fallback for unsupported types

obj = [[Custom()] * 1000] * 10  # contains unsupported objects
for _ in range(n):
    dumps(obj, default, OPT_SERIALIZE_NUMPY)

Here, `OPT_SERIALIZE_NUMPY` enables numpy serialization, while the `default` function handles other unknown types gracefully.

Relationship to Parent Topic and Other Subtopics

This subtopic builds upon the **Custom Serialization Support** main topic by providing a targeted, optimized path for numpy data types, which are a common source of serialization issues in scientific Python applications.

Diagram

flowchart TD
    UserCalls[Dumps called with Python object]
    CheckNumpy{Is object a numpy type?}
    SerializeNumpy[Convert numpy to JSON array/scalar]
    FallbackCheck{Fallback serializer provided?}
    CallFallback[Invoke fallback function]
    RaiseError[Raise serialization error]
    OutputJSON[Return JSON bytes]

    UserCalls --> CheckNumpy
    CheckNumpy -- Yes --> SerializeNumpy --> OutputJSON
    CheckNumpy -- No --> FallbackCheck
    FallbackCheck -- Yes --> CallFallback --> OutputJSON
    FallbackCheck -- No --> RaiseError

This flowchart highlights how the serialization process prioritizes numpy serialization when enabled, then falls back to custom user handlers or errors otherwise.