test_subclass.py


Overview

The [test_subclass.py](/projects/287/67683) file is a test suite designed to verify the behavior of the `orjson` JSON serialization library when handling subclassed built-in Python types. It focuses on ensuring that subclasses of fundamental types like `str`, `int`, `dict`, `list`, `float`, and `tuple` interact correctly with [orjson.dumps()](/projects/287/67747) under various conditions.

This file includes two primary test classes:

The tests cover correct serialization, error handling for invalid data or circular references, and differences in behavior compared to Python's standard `json` module.


Classes and Their Details

1. Subclass Definitions

These are simple subclasses of Python built-in types, used to test serialization behavior.

Class Name

Base Type

Description

`SubStr`

`str`

Subclass of string

`SubInt`

`int`

Subclass of integer

`SubDict`

`dict`

Subclass of dictionary

`SubList`

`list`

Subclass of list

`SubFloat`

`float`

Subclass of float

`SubTuple`

`tuple`

Subclass of tuple

These classes have no additional methods or properties and exist solely to test subclass serialization behavior.


2. TestSubclass

This class contains multiple test methods that check how [orjson.dumps()](/projects/287/67747) handles subclassed objects without any special options.

Methods


3. TestSubclassPassthrough

This class tests serialization behavior when [orjson.OPT_PASSTHROUGH_SUBCLASS](/projects/287/67684) option is enabled, which instructs `orjson` to not implicitly treat subclasses as their base types.

Methods

**Summary:** When passthrough mode is enabled, `orjson` does not attempt to convert subclass instances to their base types and instead raises errors, enforcing stricter type serialization.


Important Implementation Details and Algorithms


Interaction with Other Parts of the System

This test file is likely part of a broader test suite for the `orjson` package or an application using `orjson` where subclass serialization behavior is critical.


Usage Examples

import orjson
from test_subclass import SubStr, SubInt

# Serialize a subclassed string
data = SubStr("example")
json_bytes = orjson.dumps(data)
print(json_bytes)  # Output: b'"example"'

# Serialize a subclassed integer
num = SubInt(42)
json_bytes = orjson.dumps(num)
print(json_bytes)  # Output: b'42'

# Attempt serialization with invalid surrogate (raises JSONEncodeError)
try:
    invalid_str = SubStr("\ud800")
    orjson.dumps(invalid_str)
except orjson.JSONEncodeError:
    print("Invalid Unicode surrogate detected.")

Mermaid Class Diagram

classDiagram
    class SubStr {
    }
    class SubInt {
    }
    class SubDict {
    }
    class SubList {
    }
    class SubFloat {
    }
    class SubTuple {
    }
    class TestSubclass {
        +test_subclass_str()
        +test_subclass_str_invalid()
        +test_subclass_int()
        +test_subclass_int_64()
        +test_subclass_int_53()
        +test_subclass_dict()
        +test_subclass_list()
        +test_subclass_float()
        +test_subclass_tuple()
        +test_namedtuple()
        +test_subclass_circular_dict()
        +test_subclass_circular_list()
        +test_subclass_circular_nested()
    }
    class TestSubclassPassthrough {
        +test_subclass_str()
        +test_subclass_int()
        +test_subclass_dict()
        +test_subclass_list()
    }

    SubStr --|> str
    SubInt --|> int
    SubDict --|> dict
    SubList --|> list
    SubFloat --|> float
    SubTuple --|> tuple

    TestSubclass ..> SubStr : tests
    TestSubclass ..> SubInt : tests
    TestSubclass ..> SubDict : tests
    TestSubclass ..> SubList : tests
    TestSubclass ..> SubFloat : tests
    TestSubclass ..> SubTuple : tests
    TestSubclass ..> collections.namedtuple : tests

    TestSubclassPassthrough ..> SubStr : tests
    TestSubclassPassthrough ..> SubInt : tests
    TestSubclassPassthrough ..> SubDict : tests
    TestSubclassPassthrough ..> SubList : tests

Summary

[test_subclass.py](/projects/287/67683) is a specialized test suite verifying the serialization capabilities and limitations of `orjson` with respect to subclassed Python built-in types. It ensures that `orjson` can serialize these subclasses correctly or raise appropriate errors when encountering unsupported cases, especially under strict serialization options or passthrough mode. The tests also demonstrate `orjson`'s behavior differences compared to Python's standard `json` library and validate handling of edge cases like circular references and invalid Unicode data.