Skip to content

Commit 373e3bb

Browse files
authored
Use orjson to de-/serialize json-rpc messages (#2513)
1 parent 603632e commit 373e3bb

File tree

4 files changed

+11
-1
lines changed

4 files changed

+11
-1
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ jobs:
4141
python-version: '3.8'
4242
- run: sudo apt update
4343
- run: sudo apt install --no-install-recommends -y x11-xserver-utils
44-
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 --user
44+
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 orjson==3.10.7 --user
4545
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
4646
- run: mypy stubs
4747
- run: flake8 plugin tests

dependencies.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
">=4096": [
44
"bracex",
55
"mdpopups",
6+
"orjson",
67
"typing_extensions",
78
"wcmatch"
89
]

plugin/core/transports.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
import time
1818
import weakref
1919

20+
try:
21+
import orjson
22+
except ImportError:
23+
orjson = None
2024

2125
T = TypeVar('T')
2226
T_contra = TypeVar('T_contra', contravariant=True)
@@ -80,6 +84,8 @@ def read_data(self, reader: IO[bytes]) -> dict[str, Any] | None:
8084

8185
@staticmethod
8286
def _encode(data: dict[str, Any]) -> bytes:
87+
if orjson:
88+
return orjson.dumps(data)
8389
return json.dumps(
8490
data,
8591
ensure_ascii=False,
@@ -90,6 +96,8 @@ def _encode(data: dict[str, Any]) -> bytes:
9096

9197
@staticmethod
9298
def _decode(message: bytes) -> dict[str, Any]:
99+
if orjson:
100+
return orjson.loads(message)
93101
return json.loads(message.decode('utf-8'))
94102

95103

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ deps =
2323
mypy==1.7.1
2424
flake8==5.0.4
2525
pyright==1.1.381
26+
orjson==3.10.7
2627
commands =
2728
# mypy disabled for main code as it doesn't currently support cyclic definitions - https://github.com/python/mypy/issues/731
2829
mypy stubs

0 commit comments

Comments
 (0)