Skip to content

Commit bfa578c

Browse files
committed
Replace ujson with orjson for JSON serialization
- Update pyproject.toml dependencies to require orjson instead of ujson. - Update import fallbacks in pylsp/python_lsp.py, pylsp/__main__.py, and pylsp/plugins/pylint_lint.py. - Handle orjson-specific behavior (bytes output in dumps, absence of load method). Fixes: #673 Signed-off-by: Matěj Cepl <[email protected]>
1 parent 926f80d commit bfa578c

File tree

4 files changed

+12
-6
lines changed

4 files changed

+12
-6
lines changed

pylsp/__main__.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import time
99

1010
try:
11-
import ujson as json
11+
import orjson as json
1212
except Exception:
1313
import json
1414

@@ -99,7 +99,10 @@ def _configure_logger(verbose=0, log_config=None, log_file=None) -> None:
9999

100100
if log_config:
101101
with open(log_config, encoding="utf-8") as f:
102-
logging.config.dictConfig(json.load(f))
102+
if json.__name__ == "orjson":
103+
logging.config.dictConfig(json.loads(f.read()))
104+
else:
105+
logging.config.dictConfig(json.load(f))
103106
else:
104107
formatter = logging.Formatter(LOG_FORMAT)
105108
if log_file:

pylsp/plugins/pylint_lint.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
from pylsp import hookimpl, lsp
1616

1717
try:
18-
import ujson as json
18+
import orjson as json
1919
except Exception:
2020
import json
2121

pylsp/python_lsp.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
from typing import Any
1212

1313
try:
14-
import ujson as json
14+
import orjson as json
1515
except Exception:
1616
import json
1717

@@ -152,7 +152,10 @@ async def pylsp_ws(websocket):
152152
def send_message(message, websocket):
153153
"""Handler to send responses of processed requests to respective web socket clients"""
154154
try:
155-
payload = json.dumps(message, ensure_ascii=False)
155+
if json.__name__ == "orjson":
156+
payload = json.dumps(message).decode("utf-8")
157+
else:
158+
payload = json.dumps(message, ensure_ascii=False)
156159
loop.call_soon_threadsafe(send_queue.put_nowait, (payload, websocket))
157160
except Exception as e:
158161
log.exception("Failed to write message %s, %s", message, str(e))

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ dependencies = [
1919
"jedi>=0.17.2,<0.20.0",
2020
"pluggy>=1.0.0",
2121
"python-lsp-jsonrpc>=1.1.0,<2.0.0",
22-
"ujson>=3.0.0",
22+
"orjson",
2323
"black"
2424
]
2525
dynamic = ["version"]

0 commit comments

Comments
 (0)