Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use orjson to de-/serialize json-rpc messages #2513

Merged
merged 4 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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