Skip to content

Commit

Permalink
Use orjson to de-/serialize json-rpc messages (#2513)
Browse files Browse the repository at this point in the history
  • Loading branch information
deathaxe authored Sep 30, 2024
1 parent 603632e commit 373e3bb
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
python-version: '3.8'
- run: sudo apt update
- run: sudo apt install --no-install-recommends -y x11-xserver-utils
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 --user
- run: pip3 install mypy==1.7.1 flake8==5.0.4 pyright==1.1.381 orjson==3.10.7 --user
- run: echo "$HOME/.local/bin" >> $GITHUB_PATH
- run: mypy stubs
- run: flake8 plugin tests
Expand Down
1 change: 1 addition & 0 deletions dependencies.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
">=4096": [
"bracex",
"mdpopups",
"orjson",
"typing_extensions",
"wcmatch"
]
Expand Down
8 changes: 8 additions & 0 deletions plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import time
import weakref

try:
import orjson
except ImportError:
orjson = None

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

@staticmethod
def _encode(data: dict[str, Any]) -> bytes:
if orjson:
return orjson.dumps(data)
return json.dumps(
data,
ensure_ascii=False,
Expand All @@ -90,6 +96,8 @@ def _encode(data: dict[str, Any]) -> bytes:

@staticmethod
def _decode(message: bytes) -> dict[str, Any]:
if orjson:
return orjson.loads(message)
return json.loads(message.decode('utf-8'))


Expand Down
1 change: 1 addition & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ deps =
mypy==1.7.1
flake8==5.0.4
pyright==1.1.381
orjson==3.10.7
commands =
# mypy disabled for main code as it doesn't currently support cyclic definitions - https://github.com/python/mypy/issues/731
mypy stubs
Expand Down

0 comments on commit 373e3bb

Please sign in to comment.