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 3 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
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
10 changes: 9 additions & 1 deletion plugin/core/transports.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
from queue import Queue
from typing import Any, Callable, Dict, Generic, IO, Protocol, TypeVar
import http
import json
rchl marked this conversation as resolved.
Show resolved Hide resolved
import os
import shutil
import socket
Expand All @@ -17,6 +16,11 @@
import time
import weakref

try:
import orjson
except ImportError:
import json
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
Loading