diff --git a/setup.cfg b/setup.cfg index 60417f4..060a555 100644 --- a/setup.cfg +++ b/setup.cfg @@ -26,14 +26,6 @@ package_dir = =src packages = canonicaljson -install_requires = - # simplejson versions before 3.14.0 had a bug with some characters - # (e.g. \u2028) if ensure_ascii was set to false. - simplejson>=3.14.0 - # typing.Protocol was only added to the stdlib in Python 3.8 - typing_extensions>=4.0.0; python_version < '3.8' - - [options.package_data] canonicaljson = py.typed diff --git a/src/canonicaljson/__init__.py b/src/canonicaljson/__init__.py index 24ed332..3861434 100644 --- a/src/canonicaljson/__init__.py +++ b/src/canonicaljson/__init__.py @@ -14,13 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. import functools -import platform -from typing import Any, Callable, Generator, Iterator, Type, TypeVar - -try: - from typing import Protocol -except ImportError: # pragma: no cover - from typing_extensions import Protocol # type: ignore[assignment] +import json +from typing import Callable, Generator, Type, TypeVar __version__ = "1.6.5" @@ -63,54 +58,21 @@ def register_preserialisation_callback( _preprocess_for_serialisation.register(data_type, callback) -class Encoder(Protocol): # pragma: no cover - def encode(self, data: object) -> str: - pass - - def iterencode(self, data: object) -> Iterator[str]: - pass - - def __init__(self, *args: Any, **kwargs: Any) -> None: - pass - - -class JsonLibrary(Protocol): # pragma: no cover - @property - def JSONEncoder(self) -> Type[Encoder]: - pass - - -# Declare these in the module scope, but they get configured in -# set_json_library. -_canonical_encoder: Encoder = None # type: ignore[assignment] -_pretty_encoder: Encoder = None # type: ignore[assignment] - - -def set_json_library(json_lib: JsonLibrary) -> None: - """ - Set the underlying JSON library that canonicaljson uses to json_lib. - - Params: - json_lib: The module to use for JSON encoding. Must have a - `JSONEncoder` property. - """ - global _canonical_encoder - _canonical_encoder = json_lib.JSONEncoder( - ensure_ascii=False, - allow_nan=False, - separators=(",", ":"), - sort_keys=True, - default=_preprocess_for_serialisation, - ) - - global _pretty_encoder - _pretty_encoder = json_lib.JSONEncoder( - ensure_ascii=False, - allow_nan=False, - indent=4, - sort_keys=True, - default=_preprocess_for_serialisation, - ) +# Declare these once for re-use. +_canonical_encoder = json.JSONEncoder( + ensure_ascii=False, + allow_nan=False, + separators=(",", ":"), + sort_keys=True, + default=_preprocess_for_serialisation, +) +_pretty_encoder = json.JSONEncoder( + ensure_ascii=False, + allow_nan=False, + indent=4, + sort_keys=True, + default=_preprocess_for_serialisation, +) def encode_canonical_json(data: object) -> bytes: @@ -152,20 +114,3 @@ def iterencode_pretty_printed_json(data: object) -> Generator[bytes, None, None] """ for chunk in _pretty_encoder.iterencode(data): yield chunk.encode("utf-8") - - -if platform.python_implementation() == "PyPy": # pragma: no cover - # pypy ships with an optimised JSON encoder/decoder that is faster than - # simplejson's C extension. - import json -else: # pragma: no cover - # using simplejson rather than regular json on CPython for backwards - # compatibility (simplejson on Python 3.5 handles parsing of bytes while - # the standard library json does not). - # - # Note that it seems performance is on par or better using json from the - # standard library as of Python 3.7. - import simplejson as json # type: ignore[no-redef] - -# Set the JSON library to the backwards compatible version. -set_json_library(json) diff --git a/tests/test_canonicaljson.py b/tests/test_canonicaljson.py index 44422d5..eef6b18 100644 --- a/tests/test_canonicaljson.py +++ b/tests/test_canonicaljson.py @@ -22,12 +22,10 @@ encode_pretty_printed_json, iterencode_canonical_json, iterencode_pretty_printed_json, - set_json_library, register_preserialisation_callback, ) import unittest -from unittest import mock class TestCanonicalJson(unittest.TestCase): @@ -140,19 +138,6 @@ def test_invalid_float_values(self) -> None: with self.assertRaises(ValueError): encode_pretty_printed_json(nan) - def test_set_json(self) -> None: - """Ensure that changing the underlying JSON implementation works.""" - mock_json = mock.Mock(spec=["JSONEncoder"]) - mock_json.JSONEncoder.return_value.encode.return_value = "sentinel" - try: - set_json_library(mock_json) - self.assertEqual(encode_canonical_json({}), b"sentinel") - finally: - # Reset the JSON library to whatever was originally set. - from canonicaljson import json # type: ignore[attr-defined] - - set_json_library(json) - def test_encode_unknown_class_raises(self) -> None: class C: pass diff --git a/tox.ini b/tox.ini index 63b9d58..0c166ab 100644 --- a/tox.ini +++ b/tox.ini @@ -33,7 +33,5 @@ commands = python -m black --check --diff src tests [testenv:mypy] deps = mypy==1.0 - types-simplejson==3.17.5 types-setuptools==57.4.14 commands = mypy src tests -