Skip to content

Commit

Permalink
Replace defunct ujson library with orjson (#126)
Browse files Browse the repository at this point in the history
* Replace defunct `ujson` library with `orjson`

* Fix

* orjson doesn't serialize bytes so don't pass it bytes
  • Loading branch information
gtopper authored Aug 25, 2024
1 parent 11d3dde commit 8905401
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
requests>=2.19.1
future>=0.18.2
ujson>=3
orjson>=3.9.15, <4
4 changes: 2 additions & 2 deletions v3io/aio/dataplane/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import os
import sys

import ujson
import orjson

import v3io.aio.dataplane.transport.aiohttp
import v3io.common.helpers
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions v3io/dataplane/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import sys

import future.utils
import ujson
import orjson

import v3io.common.helpers
import v3io.dataplane.batch
Expand Down Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion v3io/dataplane/kv_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
8 changes: 4 additions & 4 deletions v3io/dataplane/request.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
except BaseException:
from urllib import urlencode, quote

import ujson
import orjson

import v3io.common.helpers
import v3io.dataplane.kv_array
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions v3io/dataplane/response.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#
import xml.etree.ElementTree

import ujson
import orjson

import v3io.dataplane.transport

Expand Down Expand Up @@ -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:
Expand Down

0 comments on commit 8905401

Please sign in to comment.