diff options
| author | J08nY | 2023-04-25 19:16:21 +0200 |
|---|---|---|
| committer | J08nY | 2023-04-25 19:16:21 +0200 |
| commit | 0a8cd2648012608ee1ba0d129608a0856a01f289 (patch) | |
| tree | 94b82b41fa5a6f0b9c0fe4105bc5dac5d785a90b /src | |
| parent | 7d4e1a1fb411313516901938ad690f4cb64ea2ee (diff) | |
| download | sec-certs-0a8cd2648012608ee1ba0d129608a0856a01f289.tar.gz sec-certs-0a8cd2648012608ee1ba0d129608a0856a01f289.tar.zst sec-certs-0a8cd2648012608ee1ba0d129608a0856a01f289.zip | |
Do not put the whole JSON into memory when writing it compressed.
Diffstat (limited to 'src')
| -rw-r--r-- | src/sec_certs/serialization/json.py | 13 |
1 files changed, 5 insertions, 8 deletions
diff --git a/src/sec_certs/serialization/json.py b/src/sec_certs/serialization/json.py index 314352bb..5add6edc 100644 --- a/src/sec_certs/serialization/json.py +++ b/src/sec_certs/serialization/json.py @@ -67,9 +67,8 @@ class ComplexSerializableType: # false positive MyPy warning, cannot be None if compress: - with gzip.open(str(output_path), "w") as handle: # type: ignore - json_str = json.dumps(self, indent=4, cls=CustomJSONEncoder, ensure_ascii=False) - handle.write(json_str.encode("utf-8")) + with gzip.open(str(output_path), "wt", encoding="utf-8") as handle: # type: ignore + json.dump(self, handle, indent=4, cls=CustomJSONEncoder, ensure_ascii=False) else: with Path(output_path).open("w") as handle: # type: ignore json.dump(self, handle, indent=4, cls=CustomJSONEncoder, ensure_ascii=False) @@ -83,12 +82,10 @@ class ComplexSerializableType: :return T: the deserialized object """ if is_compressed: - with gzip.open(str(input_path)) as handle: - json_str = handle.read().decode("utf-8") - return json.loads(json_str, cls=CustomJSONDecoder) + with gzip.open(str(input_path), "rt", encoding="utf-8") as handle: + return json.load(handle, cls=CustomJSONDecoder) else: - input_path = Path(input_path) - with input_path.open("r") as handle: + with Path(input_path).open("r") as handle: return json.load(handle, cls=CustomJSONDecoder) |
