From 8905401132e239ce384249712bd412f2b7f746c4 Mon Sep 17 00:00:00 2001 From: Gal Topper Date: Sun, 25 Aug 2024 18:40:09 +0800 Subject: [PATCH] Replace defunct `ujson` library with `orjson` (#126) * Replace defunct `ujson` library with `orjson` * Fix * orjson doesn't serialize bytes so don't pass it bytes --- requirements.txt | 2 +- v3io/aio/dataplane/client.py | 4 ++-- v3io/dataplane/client.py | 4 ++-- v3io/dataplane/kv_array.py | 2 +- v3io/dataplane/request.py | 8 ++++---- v3io/dataplane/response.py | 4 ++-- 6 files changed, 12 insertions(+), 12 deletions(-) diff --git a/requirements.txt b/requirements.txt index 60773ec..e190408 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,3 +1,3 @@ requests>=2.19.1 future>=0.18.2 -ujson>=3 +orjson>=3.9.15, <4 diff --git a/v3io/aio/dataplane/client.py b/v3io/aio/dataplane/client.py index b73367d..7f0c285 100644 --- a/v3io/aio/dataplane/client.py +++ b/v3io/aio/dataplane/client.py @@ -15,7 +15,7 @@ import os import sys -import ujson +import orjson import v3io.aio.dataplane.transport.aiohttp import v3io.common.helpers @@ -97,7 +97,7 @@ def _create_logger(self, logger_verbosity): @staticmethod def _get_schema_contents(key, fields): - return ujson.dumps({"hashingBucketNum": 0, "key": key, "fields": fields}) + return orjson.dumps({"hashingBucketNum": 0, "key": key, "fields": fields}) def _create_models(self): import v3io.aio.dataplane.container diff --git a/v3io/dataplane/client.py b/v3io/dataplane/client.py index a61b2d9..cfbb95e 100644 --- a/v3io/dataplane/client.py +++ b/v3io/dataplane/client.py @@ -16,7 +16,7 @@ import sys import future.utils -import ujson +import orjson import v3io.common.helpers import v3io.dataplane.batch @@ -1032,7 +1032,7 @@ def _ensure_path_ends_with_slash(path): @staticmethod def _get_schema_contents(key, fields): - return ujson.dumps({"hashingBucketNum": 0, "key": key, "fields": fields}) + return orjson.dumps({"hashingBucketNum": 0, "key": key, "fields": fields}) def _create_logger(self, logger_verbosity): logger = v3io.logger.Logger(level=logger_verbosity or "INFO") diff --git a/v3io/dataplane/kv_array.py b/v3io/dataplane/kv_array.py index a1670c2..11f2325 100644 --- a/v3io/dataplane/kv_array.py +++ b/v3io/dataplane/kv_array.py @@ -39,7 +39,7 @@ def encode_array(array_value, typecode): "II" + typecode * num_items, num_items * 8, operand_type, *array_value ) - return base64.b64encode(encoded_array) + return base64.b64encode(encoded_array).decode("utf-8") def decode(encoded_array): diff --git a/v3io/dataplane/request.py b/v3io/dataplane/request.py index f1ebc09..68f80e3 100644 --- a/v3io/dataplane/request.py +++ b/v3io/dataplane/request.py @@ -24,7 +24,7 @@ except BaseException: from urllib import urlencode, quote -import ujson +import orjson import v3io.common.helpers import v3io.dataplane.kv_array @@ -415,7 +415,7 @@ def _to_base64(input): if isinstance(input, str): input = input.encode("utf-8") - return base64.b64encode(input) + return base64.b64encode(input).decode("utf-8") def _dict_to_typed_attributes(d): @@ -448,7 +448,7 @@ def _dict_to_typed_attributes(d): type_value = str(value) elif attribute_type in [bytes, bytearray]: type_key = "B" - type_value = base64.b64encode(value) + type_value = base64.b64encode(value).decode("utf-8") elif isinstance(value, bool): type_key = "BOOL" type_value = value @@ -477,7 +477,7 @@ def _resolve_body_and_headers(access_key, headers, body): if not isinstance(body, dict): return headers, body - body = ujson.dumps(body, reject_bytes=False) + body = orjson.dumps(body) headers["Content-Type"] = "application/json" return headers, body diff --git a/v3io/dataplane/response.py b/v3io/dataplane/response.py index 3bf0705..218908b 100644 --- a/v3io/dataplane/response.py +++ b/v3io/dataplane/response.py @@ -14,7 +14,7 @@ # import xml.etree.ElementTree -import ujson +import orjson import v3io.dataplane.transport @@ -48,7 +48,7 @@ def output(self): # TODO: It's expensive to always try to parse as JSON first. Better # use headers or a heuristic to decide the format. try: - parsed_output = ujson.loads(self.body) + parsed_output = orjson.loads(self.body) except Exception: parsed_output = xml.etree.ElementTree.fromstring(self.body) except Exception: