json-rpc-rs
A framework-agnostic async Rust implementation of JSON-RPC 2.0 with Bring Your Own Transport. Process JSON-RPC messages with async/await support and full specification compliance.
Features
- Configure handlers with the builder pattern
- Register methods with closures and automatic parameter deserialization
- Process messages asynchronously with tokio
- Bring Your Own Transport: integrate with stdio, HTTP, WebSocket, TCP, or any custom transport
- Support requests, responses, notifications, and errors
- Handle batch requests
- Create errors with JSON-RPC compliant codes
Installation
Add this to your Cargo.toml:
[]
= "0.2"
= { = "1", = ["rt", "io-util"] }
For axum integration, enable the feature:
[]
= { = "0.2", = ["axum"] }
Quick Start
Create a JSON-RPC handler and process messages. Since this library uses Bring
Your Own Transport, you read JSON strings from your transport, call
json_rpc.call(), and write the response back.
use JsonRpc;
use Value;
async
async
Usage
Creating a Handler
use JsonRpc;
let json_rpc = new;
Registering Methods
Register methods with tuple parameters:
json_rpc.add;
Register methods with a single parameter:
json_rpc.add;
Register methods with struct parameters:
use Deserialize;
json_rpc.add;
Processing Messages
The call() method processes JSON-RPC messages. Pass a JSON string from your
transport, get back a JSON response string. Returns None for notifications
(requests without id). This is the Bring Your Own Transport pattern in action.
match json_rpc.call.await
Example: Stdio
Read newline-delimited JSON from stdin and write responses to stdout:
use JsonRpc;
use AsyncBufReadExt;
let stdin = stdin;
let mut reader = new;
let mut line = Stringnew;
while reader.read_line.await? > 0
Send a request:
|
Example: Axum
The axum feature provides a handler for HTTP integration:
use ;
use ;
use Arc;
let json_rpc = new.add;
let app = new
.route
.with_state;
Send a request:
Custom Transports
The Bring Your Own Transport pattern lets you integrate with any transport or
framework. Read a JSON string, call json_rpc.call(), write the response.
use JsonRpc;
let json_rpc = new
.add;
// Read from WebSocket, TCP, custom protocol, etc.
let message = receive_message;
if let Some = json_rpc.call.await
Error Handling
Return protocol errors:
use Error;
json_rpc.add;
Return JSON-RPC standard errors:
json_rpc.add;
Batch Requests
The call() method accepts JSON arrays representing batch requests and returns
an array of responses.
|
Response:
JSON-RPC 2.0 Compliance
Implements the JSON-RPC 2.0 specification:
- Request objects with
jsonrpc,method,params, andidfields - Notification objects (requests without
id) - Response objects with
resultorerrorfields - Error objects with
code,message, and optionaldatafields - Batch requests (arrays of requests)
- Standard error codes (-32700 to -32099)
See JSON-RPC 2.0 Specification for details.
Examples
The examples directory contains working examples:
echo_stdio.rs: Echo handler using stdioecho_axum.rs: Echo handler using axum (requiresaxumfeature)basic_stdio.rs: Handler with multiple methods and error handlingbasic_axum.rs: Handler with multiple methods and error handling (requiresaxumfeature)graceful_shutdown_http.rs: Graceful shutdown with axum (requiresaxumfeature)
Run an example:
# Stdio example
|
# Axum example
Documentation
Generate and view API documentation:
License
MIT