Typing Stubs for orjson

3.6.2 · active · verified Wed Apr 15

types-orjson provides type hints (typing stubs) for the high-performance `orjson` JSON library. It is part of the Python `typeshed` project, which maintains type stubs for various third-party packages. These stubs enable static type checkers like MyPy or Pyright to validate `orjson`'s API usage, enhancing code reliability and developer experience. The current version is 3.6.2, and updates are typically released in sync with `typeshed`'s update cycle or when `orjson` has significant API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `orjson` with `types-orjson` installed. Type checkers will now understand the types returned by `orjson.loads` and expected by `orjson.dumps`, ensuring type safety in your code without needing direct `types-orjson` imports.

import orjson
from typing import Any, Dict

def process_data(data_str: str) -> Dict[str, Any]:
    # orjson.loads provides type hints thanks to types-orjson
    parsed_data: Dict[str, Any] = orjson.loads(data_str)
    
    # orjson.dumps provides type hints
    re_serialized_data: bytes = orjson.dumps(parsed_data, option=orjson.OPT_INDENT_2)
    print(re_serialized_data.decode())
    return parsed_data

if __name__ == '__main__':
    json_string = '{"name": "Alice", "age": 30, "city": "New York"}'
    result = process_data(json_string)
    print(f"Parsed data: {result}")
    # To verify types, run a type checker:
    # mypy your_script_name.py
    # or pyright your_script_name.py

view raw JSON →