lib.rs


Overview

`lib.rs` serves as the core Rust source file for the `orjson` Python extension module, which provides high-performance JSON serialization and deserialization capabilities. This file primarily handles the integration between Rust and Python by defining the Python module, exposing the serialization (`dumps`) and deserialization (`loads`) functions to Python, and managing error handling relevant to JSON processing.

The module leverages Rust's performance and safety features while interfacing directly with Python's C API (through `pyo3_ffi`) to create a seamless, efficient JSON processing backend. It also exposes several module-level constants and custom error types.


Detailed Breakdown

Modules and Imports

Macros

Key Functions


orjson_init_exec

unsafe extern "C" fn orjson_init_exec(mptr: *mut PyObject) -> c_int

PyInit_orjson

unsafe extern "C" fn PyInit_orjson() -> *mut PyModuleDef

loads

unsafe extern "C" fn loads(_self: *mut PyObject, obj: *mut PyObject) -> *mut PyObject

dumps

unsafe extern "C" fn dumps(
    _self: *mut PyObject,
    args: *const *mut PyObject,
    nargs: Py_ssize_t,
    kwnames: *mut PyObject,
) -> *mut PyObject

Error Raising Helper Functions


Important Implementation Details and Algorithms


Interaction with Other Parts of the System


Visual Diagram

The following Mermaid class diagram illustrates the main functions and their relationships in this utility-style file, focusing on FFI functions exposed to Python and error handling helpers:

classDiagram
    class lib {
        <<module>>
        +orjson_init_exec(mptr: *mut PyObject) c_int
        +PyInit_orjson() *mut PyModuleDef
        +loads(_self: *mut PyObject, obj: *mut PyObject) *mut PyObject
        +dumps(_self: *mut PyObject, args: *const *mut PyObject, nargs: Py_ssize_t, kwnames: *mut PyObject) *mut PyObject
        -raise_loads_exception(err: DeserializeError) *mut PyObject
        -raise_dumps_exception_fixed(msg: &str) *mut PyObject
        -raise_dumps_exception_dynamic(err: &str) *mut PyObject
    }
    
    lib --> deserialize : uses
    lib --> serialize : uses
    lib --> typeref : uses
    lib --> opt : uses
    lib --> util : uses

Summary

`lib.rs` is the core integration layer of the `orjson` Rust-implemented Python module, responsible for initializing the module, exposing key JSON serialization/deserialization functions, managing Python exceptions, and defining module constants. It bridges Rust's efficient JSON processing with Python's runtime via low-level FFI, carefully handling version compatibility and error propagation.

This file does not contain the core serialization or deserialization logic itself but delegates those to the respective `serialize` and `deserialize` modules, focusing on the Python interface, argument parsing, and error communication.


End of Documentation for lib.rs