test_type.py


Overview

`test_type.py` is a comprehensive test suite designed to validate the serialization and deserialization behavior of various Python data types using the `orjson` library. This file leverages the `pytest` testing framework to systematically check correctness, error handling, and edge cases of JSON encoding/decoding operations performed by `orjson`.

The tests ensure that `orjson` correctly handles JSON fragments, invalid JSON, string encoding (including long strings, Unicode, control characters, escapes, emojis), numeric types (integers, floats, special float values like NaN and Infinity), boolean values, `None` (null), lists, tuples, bytes-like objects, and error cases such as unsupported types or invalid surrogate pairs.


Contents


Class Details

class TestType

This class encapsulates all tests related to serialization (`dumps`) and deserialization (`loads`) of Python objects with `orjson`. Each method tests a particular type, value range, or edge case.

Test Methods Summary

Method Name

Description

`test_fragment`

Tests that incomplete JSON fragments raise [JSONDecodeError](/projects/287/67684).

`test_invalid`

Tests invalid JSON strings raise [JSONDecodeError](/projects/287/67684).

`test_str`

Tests simple string serialization and deserialization.

`test_str_latin1`

Tests Latin-1 character string roundtrip.

`test_str_long`

Tests very long strings with repeated patterns roundtrip.

`test_str_2mib`

Tests very large string (~2 MiB) serialization and deserialization.

`test_str_very_long`

Tests extremely long strings that trigger internal overflow handling.

`test_str_replacement`

Tests Unicode replacement character roundtrip (`�`).

`test_str_trailing_4_byte`

Tests strings with 4-byte UTF-8 characters (e.g., emojis).

`test_str_ascii_control`

Tests strings with ASCII control characters requiring escapes.

test_str_escape_quote_*

Tests escaping of double quotes at various positions within strings.

`test_str_escape_backslash_*`

Tests escaping of backslashes at various positions within strings.

test_str_escape_x32_*

Tests escaping of ASCII control character Tab (`\t`) at various positions.

`test_str_emoji`

Tests emoji character serialization/deserialization.

`test_str_emoji_escape`

Tests emoji within strings with special characters that require escaping.

`test_very_long_list`

Tests serialization of a very long list of empty lists.

`test_very_long_list_pretty`

Same as above but with indentation option enabled.

`test_very_long_dict`

Tests serialization of a very long list of empty dicts.

`test_very_long_dict_pretty`

Same as above with indentation.

`test_very_long_str_empty`

Tests serialization of very long lists of empty strings.

`test_very_long_str_empty_pretty`

Same as above with indentation.

`test_very_long_str_not_empty`

Tests serialization of very long lists of non-empty strings.

`test_very_long_str_not_empty_pretty`

Same as above with indentation.

`test_very_long_bool`

Tests serialization of very long lists of [True](/projects/287/68088) values.

`test_very_long_bool_pretty`

Same as above with indentation.

`test_very_long_int`

Tests serialization of very long lists of large integers.

`test_very_long_int_pretty`

Same as above with indentation.

`test_very_long_float`

Tests serialization of very long lists of floats.

`test_very_long_float_pretty`

Same as above with indentation.

`test_str_surrogates_loads`

Tests that invalid surrogate pairs in JSON strings raise [JSONDecodeError](/projects/287/67684) on load.

`test_str_surrogates_dumps`

Tests that invalid surrogate characters raise [JSONEncodeError](/projects/287/67684) on dump.

`test_bytes_dumps`

Tests that dumping `bytes` objects raises [JSONEncodeError](/projects/287/67684).

`test_bytes_loads`

Tests that loading JSON from bytes works correctly.

`test_bytearray_loads`

Tests loading JSON from `bytearray` objects.

`test_memoryview_loads`

Tests loading JSON from `memoryview` objects.

`test_bytesio_loads`

Tests loading JSON from [io.BytesIO](/projects/287/67789) buffers.

`test_bool`

Tests boolean serialization and deserialization.

`test_bool_true_array`

Tests serialization of a large list of [True](/projects/287/68088) values.

`test_bool_false_array`

Tests serialization of a large list of [False](/projects/287/68143) values.

`test_none`

Tests `None` serialization/deserialization as JSON `null`.

`test_int`

Tests integer array serialization/deserialization with compact JSON.

`test_null_array`

Tests serialization of large lists of `null` values.

`test_nan_dumps`

Tests that [NaN](/projects/287/68138) serializes to JSON `null`.

`test_nan_loads`

Tests that JSON containing [NaN](/projects/287/68138) raises [JSONDecodeError](/projects/287/67684).

`test_infinity_dumps`

Tests that [Infinity](/projects/287/68045) serializes to JSON `null`.

`test_infinity_loads`

Tests that JSON containing [Infinity](/projects/287/68045) raises [JSONDecodeError](/projects/287/67684).

`test_int_53`

Tests 53-bit integer roundtrip serialization/deserialization with [OPT_STRICT_INTEGER](/projects/287/67684) option.

`test_int_53_exc`

Tests that 64-bit integers outside 53-bit range raise [JSONEncodeError](/projects/287/67684) with strict integer option.

`test_int_53_exc_usize`

Tests that very large unsigned 64-bit integers raise exceptions with strict integer option.

`test_int_53_exc_128`

Tests that 128-bit integers raise [JSONEncodeError](/projects/287/67684) with strict integer option.

`test_int_64`

Tests 64-bit integer serialization/deserialization.

`test_uint_64`

Tests unsigned 64-bit integers serialization/deserialization.

`test_int_128`

Tests that 128-bit integers raise [JSONEncodeError](/projects/287/67684).

`test_float`

Tests multiple floating point values for correct deserialization.

`test_float_precision_loads`

Tests precise floating point values during deserialization.

`test_float_precision_dumps`

Tests precise floating point values during serialization.

`test_float_edge`

Tests edge cases for floating point serialization/deserialization.

`test_float_notation`

Tests scientific notation strings for deserialization and serialization behavior.

`test_list`

Tests serialization/deserialization of mixed-type list.

`test_tuple`

Tests serialization/deserialization of tuple (converted to list in JSON).

`test_object`

Tests that arbitrary objects raise [JSONEncodeError](/projects/287/67684) on serialization.


Important Implementation Details


Usage Examples

import orjson

# Serialize a Python dict to JSON bytes
data = {"name": "Alice", "age": 30}
json_bytes = orjson.dumps(data)

# Deserialize JSON bytes back to Python dict
obj = orjson.loads(json_bytes)

print(obj)
# Output: {'name': 'Alice', 'age': 30}

The test suite ensures that such common operations and their edge cases are thoroughly validated.


Interaction with Other Parts of the System

This file is primarily for **testing** and thus interacts indirectly with any system components or user code that rely on `orjson` for JSON processing.


Visual Diagram

classDiagram
    class TestType {
        +test_fragment()
        +test_invalid()
        +test_str()
        +test_str_latin1()
        +test_str_long()
        +test_str_2mib()
        +test_str_very_long()
        +test_str_replacement()
        +test_str_trailing_4_byte()
        +test_str_ascii_control()
        +test_str_escape_quote_0()
        +test_str_escape_quote_1()
        +test_str_escape_quote_2()
        +test_str_escape_quote_3()
        +test_str_escape_quote_4()
        +test_str_escape_quote_5()
        +test_str_escape_quote_6()
        +test_str_escape_quote_7()
        +test_str_escape_quote_8()
        +test_str_escape_quote_multi()
        +test_str_escape_quote_buffer()
        +test_str_escape_backslash_0()
        +test_str_escape_backslash_1()
        +test_str_escape_backslash_2()
        +test_str_escape_backslash_3()
        +test_str_escape_backslash_4()
        +test_str_escape_backslash_5()
        +test_str_escape_backslash_6()
        +test_str_escape_backslash_7()
        +test_str_escape_backslash_8()
        +test_str_escape_backslash_multi()
        +test_str_escape_backslash_buffer()
        +test_str_escape_x32_0()
        +test_str_escape_x32_1()
        +test_str_escape_x32_2()
        +test_str_escape_x32_3()
        +test_str_escape_x32_4()
        +test_str_escape_x32_5()
        +test_str_escape_x32_6()
        +test_str_escape_x32_7()
        +test_str_escape_x32_8()
        +test_str_escape_x32_multi()
        +test_str_escape_x32_buffer()
        +test_str_emoji()
        +test_str_emoji_escape()
        +test_very_long_list()
        +test_very_long_list_pretty()
        +test_very_long_dict()
        +test_very_long_dict_pretty()
        +test_very_long_str_empty()
        +test_very_long_str_empty_pretty()
        +test_very_long_str_not_empty()
        +test_very_long_str_not_empty_pretty()
        +test_very_long_bool()
        +test_very_long_bool_pretty()
        +test_very_long_int()
        +test_very_long_int_pretty()
        +test_very_long_float()
        +test_very_long_float_pretty()
        +test_str_surrogates_loads()
        +test_str_surrogates_dumps()
        +test_bytes_dumps()
        +test_bytes_loads()
        +test_bytearray_loads()
        +test_memoryview_loads()
        +test_bytesio_loads()
        +test_bool()
        +test_bool_true_array()
        +test_bool_false_array()
        +test_none()
        +test_int()
        +test_null_array()
        +test_nan_dumps()
        +test_nan_loads()
        +test_infinity_dumps()
        +test_infinity_loads()
        +test_int_53()
        +test_int_53_exc()
        +test_int_53_exc_usize()
        +test_int_53_exc_128()
        +test_int_64()
        +test_uint_64()
        +test_int_128()
        +test_float()
        +test_float_precision_loads()
        +test_float_precision_dumps()
        +test_float_edge()
        +test_float_notation()
        +test_list()
        +test_tuple()
        +test_object()
    }

Summary

`test_type.py` is a vital testing module that ensures the robustness, compliance, and correctness of JSON serialization and deserialization in the `orjson` library across a wide range of Python types and JSON edge cases. It validates `orjson`'s behavior for typical usage scenarios as well as boundary and error conditions, providing confidence in the library's reliability for JSON processing within larger applications.