Skip to content

Commit 087f467

Browse files
authored
Merge pull request #12 from opentensor/feat/thewhaleking/remove-ujson
Remove ujson
2 parents 2503908 + 4ad69cb commit 087f467

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

async_substrate_interface/async_substrate.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
from bt_decode import PortableRegistry, decode as decode_by_type_string, MetadataV15
2727
from scalecodec.base import ScaleBytes, ScaleType, RuntimeConfigurationObject
2828
from scalecodec.types import GenericCall, GenericRuntimeCallDefinition, GenericExtrinsic
29-
import ujson
3029
from websockets.asyncio.client import connect
3130
from websockets.exceptions import ConnectionClosed
3231

@@ -43,7 +42,7 @@
4342
SubstrateMixin,
4443
Preprocessed,
4544
)
46-
from async_substrate_interface.utils import hex_to_bytes
45+
from async_substrate_interface.utils import hex_to_bytes, json
4746
from async_substrate_interface.utils.storage import StorageKey
4847

4948
if TYPE_CHECKING:
@@ -575,7 +574,7 @@ async def shutdown(self):
575574
async def _recv(self) -> None:
576575
try:
577576
# TODO consider wrapping this in asyncio.wait_for and use that for the timeout logic
578-
response = ujson.loads(await self.ws.recv(decode=False))
577+
response = json.loads(await self.ws.recv(decode=False))
579578
self.last_received = time.time()
580579
async with self._lock:
581580
# note that these 'subscriptions' are all waiting sent messages which have not received
@@ -617,7 +616,7 @@ async def send(self, payload: dict) -> int:
617616
self.id += 1
618617
# self._open_subscriptions += 1
619618
try:
620-
await self.ws.send(ujson.dumps({**payload, **{"id": original_id}}))
619+
await self.ws.send(json.dumps({**payload, **{"id": original_id}}))
621620
return original_id
622621
except (ConnectionClosed, ssl.SSLError, EOFError):
623622
async with self._lock:

async_substrate_interface/sync_substrate.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from bt_decode import PortableRegistry, decode as decode_by_type_string, MetadataV15
99
from scalecodec import GenericExtrinsic, GenericCall, GenericRuntimeCallDefinition
1010
from scalecodec.base import RuntimeConfigurationObject, ScaleBytes, ScaleType
11-
import ujson
1211
from websockets.sync.client import connect
1312

1413
from async_substrate_interface.errors import (
@@ -24,7 +23,7 @@
2423
Preprocessed,
2524
ScaleObj,
2625
)
27-
from async_substrate_interface.utils import hex_to_bytes
26+
from async_substrate_interface.utils import hex_to_bytes, json
2827
from async_substrate_interface.utils.storage import StorageKey
2928

3029

@@ -1633,12 +1632,12 @@ def _make_rpc_request(
16331632
item_id = 0
16341633
for payload in payloads:
16351634
item_id += 1
1636-
ws.send(ujson.dumps({**payload["payload"], **{"id": item_id}}))
1635+
ws.send(json.dumps({**payload["payload"], **{"id": item_id}}))
16371636
request_manager.add_request(item_id, payload["id"])
16381637

16391638
while True:
16401639
try:
1641-
response = ujson.loads(
1640+
response = json.loads(
16421641
ws.recv(timeout=self.retry_timeout, decode=False)
16431642
)
16441643
except TimeoutError:

async_substrate_interface/utils/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
import importlib
2+
3+
14
def hex_to_bytes(hex_str: str) -> bytes:
25
"""
36
Converts a hex-encoded string into bytes. Handles 0x-prefixed and non-prefixed hex-encoded strings.
@@ -7,3 +10,18 @@ def hex_to_bytes(hex_str: str) -> bytes:
710
else:
811
bytes_result = bytes.fromhex(hex_str)
912
return bytes_result
13+
14+
15+
def import_json_lib():
16+
libs = ["ujson", "orjson", "simplejson", "json"]
17+
18+
for lib in libs:
19+
try:
20+
return importlib.import_module(lib)
21+
except ImportError:
22+
continue
23+
24+
raise ImportError("None of the specified JSON libraries are installed.")
25+
26+
27+
json = import_json_lib()

pyproject.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ dependencies = [
1212
"bittensor-wallet>=2.1.3",
1313
"bt-decode==0.4.0",
1414
"scalecodec~=1.2.11",
15-
"ujson",
1615
"websockets>=14.1",
1716
"xxhash"
1817
]

0 commit comments

Comments
 (0)