Add your description here
Project description
jsonrpcpeer
A lightweight, asynchronous JSON-RPC 2.0 peer implementation for Python, built on top of asyncio.
This package provides the core communication logic used by robotframework-jsonrpcremote, but it can be used independently to build custom JSON-RPC 2.0 clients and servers.
Features
- Standard Compliant: Full JSON-RPC 2.0 support (Requests, Notifications, Errors).
- Asynchronous: Built using Python's
asynciofor high performance. - Type Safe: Uses Python type hints for automatic parameter deserialization.
- Flexible: Works with any
asyncio.StreamReaderandasyncio.StreamWriter. - Data Helpers: Includes utilities for converting Python
dataclassesto/from JSON.
Protocol & Framing
While this library implements the JSON-RPC 2.0 protocol for the message payload, it uses a specific framing strategy for the transport layer, similar to the Language Server Protocol (LSP) or V8 Inspector Protocol.
Each message consists of two parts:
- Header Part: Contains the
Content-Lengthheader. - Content Part: The actual JSON-RPC message.
Example:
Content-Length: 45\r\n
\r\n
{"jsonrpc": "2.0", "method": "ping", "id": 1}
- The headers are encoded in ASCII.
- The headers are separated from the content by
\r\n\r\n. - Currently, only
Content-LengthandContent-Type(for charset) are supported/parsed.
Why use headers?
Stream-based transports (like TCP, pipes, or standard I/O) transmit data as a continuous stream of bytes rather than distinct messages. To correctly separate messages (framing), the receiver needs to know exactly where one message ends and the next begins.
While some implementations rely on newlines (which breaks with pretty-printed JSON) or try to parse balanced braces (which is complex and slow), using a Content-Length header is a robust and standard approach (used by HTTP and LSP) to determine the exact size of the message payload before reading it.
Installation
pip install jsonrpcpeer
Usage
You can find complete examples in the examples directory.
Creating a Server
You can register handlers using simple functions or organize them in a class using decorators.
1. Function-based Registration (Simple)
This approach is good for simple scripts or when you don't need complex parameter structures.
See examples/simple_server.py and examples/simple_client.py.
2. Class-based Registration (Recommended)
For larger applications, use classes and dataclasses to define your API. This example also demonstrates bidirectional communication (server calling client).
See examples/class_based_server.py and examples/class_based_client.py.
3. Typed Registration
This example shows how to use typed handlers and clients.
See examples/typed_server.py and examples/typed_client.py.
Handler Signatures
Handlers can be registered with two types of signatures:
- (params) -> result: Simple handler receiving only parameters.
- (peer, params) -> result: Handler that also receives the
JsonRpcPeerinstance (useful for context or sending callbacks).
The params argument type annotation is used to validate and convert the incoming JSON data. It supports:
- Primitive types (
str,int,bool,float) dataclassesdict,listAny
Advanced Topics
Type Conversion & Validation
jsonrpcpeer leverages Python's type hints to ensure that the data received from the remote peer matches what your handler expects.
- Strict Types: The library generally expects the incoming JSON types to match the Python type hints (e.g.,
intexpects a JSON number,strexpects a JSON string). - Dataclasses: You can define complex data structures using
dataclasses. The library will automatically map the incoming JSON object to your dataclass fields. - Validation: If the incoming data cannot be converted to the specified type (e.g., missing fields in a dataclass, or wrong type), the library automatically responds with a JSON-RPC
Invalid Paramserror (-32602).
Error Handling
Exceptions raised within your request handlers are automatically caught and translated into JSON-RPC Error responses.
- Standard Exceptions: Any unhandled exception results in an
Internal Error(-32603) with the exception message. - Custom Errors: You can raise
JsonRpcError(or subclasses) to return specific error codes and data to the client.
Bidirectional Communication
JSON-RPC 2.0 is a peer-to-peer protocol. Although we often use the terms "Client" and "Server":
- Client: Typically initiates the connection.
- Server: Accepts the connection.
Once connected, both sides can send requests and notifications to the other. This is useful for:
- Server-side Events: The server sending notifications to the client (e.g., progress updates, log messages).
- Callbacks: The server requesting information from the client during a procedure call.
License
Apache-2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file jsonrpcpeer-0.1.0.tar.gz.
File metadata
- Download URL: jsonrpcpeer-0.1.0.tar.gz
- Upload date:
- Size: 12.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aaabe7ad3350828d182642b09a27be4b55cd43b5c60d863ffd69500496f3a979
|
|
| MD5 |
7e266385a07f6e5aa9f6254aa8226c6f
|
|
| BLAKE2b-256 |
b479d314daf36a014dc887e5db8b0d4ef7f867c5a95ac36094684f4068c5b6b1
|
File details
Details for the file jsonrpcpeer-0.1.0-py3-none-any.whl.
File metadata
- Download URL: jsonrpcpeer-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.24 {"installer":{"name":"uv","version":"0.9.24","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c1733c143d4445a689fe9401899589e8ad472386f5afe2fc3a0d5d15a5c9afed
|
|
| MD5 |
80eb1cf9f686a22d7f784f9393dc6a01
|
|
| BLAKE2b-256 |
1f6dc9d7e31c7384581a635df6db695a4d8a43d8a48bd4f7fd58aa14f7cdf1cc
|