|
14 | 14 | # See the License for the specific language governing permissions and |
15 | 15 | # limitations under the License. |
16 | 16 | 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 |
24 | 19 |
|
25 | 20 |
|
26 | 21 | __version__ = "1.6.5" |
@@ -63,54 +58,21 @@ def register_preserialisation_callback( |
63 | 58 | _preprocess_for_serialisation.register(data_type, callback) |
64 | 59 |
|
65 | 60 |
|
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 | +) |
114 | 76 |
|
115 | 77 |
|
116 | 78 | def encode_canonical_json(data: object) -> bytes: |
@@ -152,20 +114,3 @@ def iterencode_pretty_printed_json(data: object) -> Generator[bytes, None, None] |
152 | 114 | """ |
153 | 115 | for chunk in _pretty_encoder.iterencode(data): |
154 | 116 | 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) |
0 commit comments