From 0ac6e957ccacd3c8326f03e8abd13ccd4e42afc7 Mon Sep 17 00:00:00 2001 From: Nice Zombies Date: Mon, 3 Feb 2025 09:13:27 +0100 Subject: [PATCH] Test types --- README.md | 2 +- docs/source/how-to.rst | 2 ++ docs/source/index.rst | 2 +- src/jsonyx/test/test_dumps.py | 42 +++++++++++++++++++++++++++++++++-- 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index ad54b13..75776c0 100644 --- a/README.md +++ b/README.md @@ -46,7 +46,7 @@ https://jsonyx.readthedocs.io ``` - Dedicated functions for reading and writing files and pretty printing -## Benchmark +## Benchmark (Oct 13, 2024) We recommend to use [`orjson`](https://pypi.org/project/orjson), [`msgspec`](https://pypi.org/project/msgspec) or diff --git a/docs/source/how-to.rst b/docs/source/how-to.rst index 14ecefa..2d0ec95 100644 --- a/docs/source/how-to.rst +++ b/docs/source/how-to.rst @@ -40,6 +40,8 @@ Encoding :mod:`numpy` objects >>> json.dump(obj, types=types) [false, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0.0, 0.0, 0.0, 0.0] +.. note:: If needed, you can also specify ``"mapping"`` or ``"str"``. + Specializing JSON object encoding --------------------------------- diff --git a/docs/source/index.rst b/docs/source/index.rst index 0590354..77ac9ac 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -49,7 +49,7 @@ performance and no dependencies. - Dedicated functions for reading and writing files and pretty printing -.. rubric:: Benchmark +.. rubric:: Benchmark (Oct 13, 2024) We recommend to use :pypi:`orjson`, :pypi:`msgspec` or :pypi:`pysimdjson` for performance critical applications: diff --git a/src/jsonyx/test/test_dumps.py b/src/jsonyx/test/test_dumps.py index 77ca958..6e33eef 100644 --- a/src/jsonyx/test/test_dumps.py +++ b/src/jsonyx/test/test_dumps.py @@ -1,11 +1,10 @@ # Copyright (C) 2024 Nice Zombies """JSON dumps tests.""" -# TODO(Nice Zombies): test types from __future__ import annotations __all__: list[str] = [] -from collections import UserDict, UserList +from collections import UserDict, UserList, UserString from decimal import Decimal from enum import Enum from typing import TYPE_CHECKING @@ -25,6 +24,24 @@ _CIRCULAR_LIST.append(_CIRCULAR_LIST) +# pylint: disable-next=R0903 +class _MyBool: + def __bool__(self) -> bool: + return False + + +# pylint: disable-next=R0903 +class _MyInt: + def __int__(self) -> int: + return 0 + + +# pylint: disable-next=R0903 +class _MyFloat: + def __float__(self) -> float: + return 0.0 + + class _FloatEnum(float, Enum): ZERO = 0.0 @@ -45,6 +62,11 @@ def test_singletons( assert json.dumps(obj, end="") == expected +def test_bool_types(json: ModuleType) -> None: + """Test bool_types.""" + assert json.dumps(_MyBool(), end="", types={"bool": _MyBool}) == "false" + + @pytest.mark.parametrize("num", [0, 1]) @pytest.mark.parametrize("num_type", [Decimal, int]) def test_int( @@ -54,6 +76,11 @@ def test_int( assert json.dumps(num_type(num), end="") == repr(num) +def test_int_types(json: ModuleType) -> None: + """Test int_types.""" + assert json.dumps(_MyInt(), end="", types={"int": _MyInt}) == "0" + + @pytest.mark.parametrize("num_type", [Decimal, float]) def test_rational_number( json: ModuleType, num_type: type[Decimal | float], @@ -100,6 +127,11 @@ def test_signaling_nan(json: ModuleType) -> None: json.dumps(Decimal("sNaN")) +def test_float_types(json: ModuleType) -> None: + """Test float_types.""" + assert json.dumps(_MyFloat(), end="", types={"float": _MyFloat}) == "0.0" + + @pytest.mark.parametrize(("obj", "expected"), [ (_IntEnum.ZERO, "0"), (_FloatEnum.ZERO, "0.0"), @@ -187,6 +219,12 @@ def test_surrogate_escapes_not_allowed(json: ModuleType, obj: str) -> None: json.dumps(obj, ensure_ascii=True) +def test_str_types(json: ModuleType) -> None: + """Test str_types.""" + obj: UserString = UserString("") + assert json.dumps(obj, end="", types={"str": UserString}) == '""' + + @pytest.mark.parametrize(("obj", "expected"), [ # Empty list ([], "[]"),