Skip to content

Commit

Permalink
Test types
Browse files Browse the repository at this point in the history
  • Loading branch information
nineteendo committed Feb 3, 2025
1 parent ea2000b commit 0ac6e95
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions docs/source/how-to.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
---------------------------------

Expand Down
2 changes: 1 addition & 1 deletion docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
42 changes: 40 additions & 2 deletions src/jsonyx/test/test_dumps.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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

Expand All @@ -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(
Expand All @@ -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],
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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
([], "[]"),
Expand Down

0 comments on commit 0ac6e95

Please sign in to comment.