| Crates.io | jrpc-types |
| lib.rs | jrpc-types |
| version | 0.2.0 |
| created_at | 2025-06-21 15:54:12.702693+00 |
| updated_at | 2025-07-19 01:37:45.74293+00 |
| description | A simple but super helpful crate for (de)serializing JSON-RPC 2.0 objects. |
| homepage | https://github.com/PrintPractical/jrpc |
| repository | |
| max_upload_size | |
| id | 1720910 |
| size | 59,830 |
This crate implements the structures as defined in the JSON-RPC 2.0 Specification
These requirements derived from the specification itself.
You can easily build a JSON-RPC request:
use jrpc_types::JsonRpcRequest;
let data = vec![10, 293, 2, 193, 2];
let req = JsonRpcRequest::builder()
.method("sort")
.params(data)
.unwrap() // Serialization of parameters could fail, so you need to catch this.
.id(2)
.build();
Deserializing a request from a string is super easy:
use jrpc_types::JsonRpcRequest;
let data = r#"{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}"#;
match TryInto::<JsonRpcRequest>::try_into(data) {
Ok(req) => {
// .. do something ..
}
Err(e) => {
// .. handle error ..
}
}
JSON-RPC Notifications are pretty much Requests, without an ID... You can build Notifications like:
use jrpc_types::JsonRpcNotification;
let data = vec![10, 293, 2, 193, 2];
let req = JsonRpcNotification::builder()
.method("event")
.params(data)
.unwrap() // Serialization of parameters could fail, so you need to catch this.
.build();
Deserializing a notification from a string is super easy:
use jrpc_types::JsonRpcNotification;
let data = r#"{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}"#;
match TryInto::<JsonRpcNotification>::try_into(data) {
Ok(req) => {
// .. do something ..
}
Err(e) => {
// .. handle error ..
}
}
A JSON-RPC response contains different fields depending on success or error. There is also a select set of (code, message) pairs for errors that are defined in the spec. The builder implementation makes it incredibly easy to build these responses.
Success response (w/ optional data):
use jrpc_types::JsonRpcResponse;
let data = vec![10, 293, 2, 193, 2];
let req = JsonRpcResponse::builder()
.success()
.result(data)
.unwrap() // Serialization of parameters could fail, so you need to catch this.
.id(2)
.build();
Invalid request error:
use jrpc_types::JsonRpcResponse;
let data = vec![10, 293, 2, 193, 2];
let req = JsonRpcResponse::builder()
.error()
.invalid_request()
.id(2)
.build();
Custom Error:
use jrpc_types::JsonRpcResponse;
let data = vec![10, 293, 2, 193, 2];
let req = JsonRpcResponse::builder()
.error()
.code(-23)
.message("bad request man...")
.id(2)
.build();
NOTE: If you are processing a JsonRpcRequest and building a JsonRpcResponse, you can use &JsonRpcRequest in the id() builder function.
use jrpc_types::{JsonRpcRequest, JsonRpcResponse};
// Just for docs, creating this here.. assume you have this.
let req = JsonRpcRequest::builder()
.id(10)
.method("subtract")
.params_str("[5,2]")
.unwrap() // Serialization of parameters could fail, so you need to catch this.
.build();
// ... process stuff ...
let rsp = JsonRpcResponse::builder()
.id(&req)
.success()
.result(3)
.unwrap() // Serialization of parameters could fail, so you need to catch this.
.build();