Skip to content

Commit 95b6ec7

Browse files
authored
Stop using simplejson. (#60)
1 parent f17afae commit 95b6ec7

File tree

4 files changed

+17
-97
lines changed

4 files changed

+17
-97
lines changed

setup.cfg

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,6 @@ package_dir = =src
2626
packages =
2727
canonicaljson
2828

29-
install_requires =
30-
# simplejson versions before 3.14.0 had a bug with some characters
31-
# (e.g. \u2028) if ensure_ascii was set to false.
32-
simplejson>=3.14.0
33-
# typing.Protocol was only added to the stdlib in Python 3.8
34-
typing_extensions>=4.0.0; python_version < '3.8'
35-
36-
3729
[options.package_data]
3830
canonicaljson = py.typed
3931

src/canonicaljson/__init__.py

Lines changed: 17 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,8 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
import functools
17-
import platform
18-
from typing import Any, Callable, Generator, Iterator, Type, TypeVar
19-
20-
try:
21-
from typing import Protocol
22-
except ImportError: # pragma: no cover
23-
from typing_extensions import Protocol # type: ignore[assignment]
17+
import json
18+
from typing import Callable, Generator, Type, TypeVar
2419

2520

2621
__version__ = "1.6.5"
@@ -63,54 +58,21 @@ def register_preserialisation_callback(
6358
_preprocess_for_serialisation.register(data_type, callback)
6459

6560

66-
class Encoder(Protocol): # pragma: no cover
67-
def encode(self, data: object) -> str:
68-
pass
69-
70-
def iterencode(self, data: object) -> Iterator[str]:
71-
pass
72-
73-
def __init__(self, *args: Any, **kwargs: Any) -> None:
74-
pass
75-
76-
77-
class JsonLibrary(Protocol): # pragma: no cover
78-
@property
79-
def JSONEncoder(self) -> Type[Encoder]:
80-
pass
81-
82-
83-
# Declare these in the module scope, but they get configured in
84-
# set_json_library.
85-
_canonical_encoder: Encoder = None # type: ignore[assignment]
86-
_pretty_encoder: Encoder = None # type: ignore[assignment]
87-
88-
89-
def set_json_library(json_lib: JsonLibrary) -> None:
90-
"""
91-
Set the underlying JSON library that canonicaljson uses to json_lib.
92-
93-
Params:
94-
json_lib: The module to use for JSON encoding. Must have a
95-
`JSONEncoder` property.
96-
"""
97-
global _canonical_encoder
98-
_canonical_encoder = json_lib.JSONEncoder(
99-
ensure_ascii=False,
100-
allow_nan=False,
101-
separators=(",", ":"),
102-
sort_keys=True,
103-
default=_preprocess_for_serialisation,
104-
)
105-
106-
global _pretty_encoder
107-
_pretty_encoder = json_lib.JSONEncoder(
108-
ensure_ascii=False,
109-
allow_nan=False,
110-
indent=4,
111-
sort_keys=True,
112-
default=_preprocess_for_serialisation,
113-
)
61+
# Declare these once for re-use.
62+
_canonical_encoder = json.JSONEncoder(
63+
ensure_ascii=False,
64+
allow_nan=False,
65+
separators=(",", ":"),
66+
sort_keys=True,
67+
default=_preprocess_for_serialisation,
68+
)
69+
_pretty_encoder = json.JSONEncoder(
70+
ensure_ascii=False,
71+
allow_nan=False,
72+
indent=4,
73+
sort_keys=True,
74+
default=_preprocess_for_serialisation,
75+
)
11476

11577

11678
def encode_canonical_json(data: object) -> bytes:
@@ -152,20 +114,3 @@ def iterencode_pretty_printed_json(data: object) -> Generator[bytes, None, None]
152114
"""
153115
for chunk in _pretty_encoder.iterencode(data):
154116
yield chunk.encode("utf-8")
155-
156-
157-
if platform.python_implementation() == "PyPy": # pragma: no cover
158-
# pypy ships with an optimised JSON encoder/decoder that is faster than
159-
# simplejson's C extension.
160-
import json
161-
else: # pragma: no cover
162-
# using simplejson rather than regular json on CPython for backwards
163-
# compatibility (simplejson on Python 3.5 handles parsing of bytes while
164-
# the standard library json does not).
165-
#
166-
# Note that it seems performance is on par or better using json from the
167-
# standard library as of Python 3.7.
168-
import simplejson as json # type: ignore[no-redef]
169-
170-
# Set the JSON library to the backwards compatible version.
171-
set_json_library(json)

tests/test_canonicaljson.py

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,10 @@
2222
encode_pretty_printed_json,
2323
iterencode_canonical_json,
2424
iterencode_pretty_printed_json,
25-
set_json_library,
2625
register_preserialisation_callback,
2726
)
2827

2928
import unittest
30-
from unittest import mock
3129

3230

3331
class TestCanonicalJson(unittest.TestCase):
@@ -140,19 +138,6 @@ def test_invalid_float_values(self) -> None:
140138
with self.assertRaises(ValueError):
141139
encode_pretty_printed_json(nan)
142140

143-
def test_set_json(self) -> None:
144-
"""Ensure that changing the underlying JSON implementation works."""
145-
mock_json = mock.Mock(spec=["JSONEncoder"])
146-
mock_json.JSONEncoder.return_value.encode.return_value = "sentinel"
147-
try:
148-
set_json_library(mock_json)
149-
self.assertEqual(encode_canonical_json({}), b"sentinel")
150-
finally:
151-
# Reset the JSON library to whatever was originally set.
152-
from canonicaljson import json # type: ignore[attr-defined]
153-
154-
set_json_library(json)
155-
156141
def test_encode_unknown_class_raises(self) -> None:
157142
class C:
158143
pass

tox.ini

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,5 @@ commands = python -m black --check --diff src tests
3333
[testenv:mypy]
3434
deps =
3535
mypy==1.0
36-
types-simplejson==3.17.5
3736
types-setuptools==57.4.14
3837
commands = mypy src tests
39-

0 commit comments

Comments
 (0)