Python LSP JSON-RPC
python-lsp-jsonrpc is a Python 3.8+ server implementation of the JSON RPC 2.0 protocol. This library was extracted from the Python LSP Server project to provide a standalone JSON-RPC core. It is currently at version 1.1.2 and receives updates as needed for bug fixes and minor feature enhancements.
Warnings
- breaking As of version 1.1.0, support for Python 3.7 and 3.6 has been dropped. Users on these Python versions must either upgrade their Python environment or pin to a previous version of `python-lsp-jsonrpc`.
- breaking Version 1.0.0 introduced a package rename. This library is a fork of Palantir's `python-jsonrpc-server` and has been renamed to `python-lsp-jsonrpc`. Code referencing the old package name will break.
- gotcha The PyPI package name is `python-lsp-jsonrpc`, but the top-level Python import module is `jsonrpc`. Attempting to import from `python_lsp_jsonrpc` will result in an `ImportError`.
- gotcha While `python-lsp-jsonrpc` is compatible with both `ujson` (for performance) and Python's built-in `json` library, ensure consistent usage if relying on specific `ujson` features or performance characteristics, as tests have specifically addressed compatibility.
Install
-
pip install -U python-lsp-jsonrpc
Imports
- JSONRPCServer
from jsonrpc.server import JSONRPCServer
- JSONRPCMethodManager
from jsonrpc.manager import JSONRPCMethodManager
Quickstart
import logging
import sys
from jsonrpc.manager import JSONRPCMethodManager
from jsonrpc.server import JSONRPCServer
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler(sys.stderr)
handler.setFormatter(logging.Formatter('%(levelname)s: %(message)s'))
logger.addHandler(handler)
class ExampleManager(JSONRPCMethodManager):
def sum(self, a, b):
logger.info(f"Received sum request for {a}, {b}")
return a + b
def notify_exit(self):
logger.info("Received exit notification.")
sys.exit(0)
def main():
manager = ExampleManager()
server = JSONRPCServer(manager)
logger.info("Starting JSON RPC server (STDIO). Send JSON RPC requests via stdin.")
server.serve_forever()
if __name__ == '__main__':
main()