jsonrpcmsg
A Rust library to serialize (encode) and deserialize (parse) JSON-RPC messages.
Features
- Supports JSON RPC v1.0, v1.1 and version 2.0 formats
- Serialize (encoder) and deserialize (parser) JSON-RPC requests, responses, and batches
- Comprehensive error handling
Use cases
- Important - It does not implement the client or server.
- JSON-RPC validator
- JSON-RPC server or client development
Installation
Add this to your Cargo.toml:
[]
= "0.1.0"
Or run:
Usage
The examples below are for JSON-RPC 2.0.
Note - For version 1 or 1.1: check in source file documentation
Working with JSON-RPC Requests
Requests Serialization
Requests serialization is performed on the client side, sending the requests.
use ;
// Create a new JSON-RPC 2.0 request with parameters
let params = Some;
let request = new;
// Serialize to JSON string
let json_string = to_request_string.unwrap;
println!;
Here's the corresponding valid JSON-RPC 2.0 request example to be sent:
Requests Deserialization (parsing)
Requests deserialization is performed on the server side, receiving the requests.
This library will deserialize a JSON string message into a Request object that can be processed by your application.
use ;
// Deserialize from JSON string
let parsed_request = from_request_string.unwrap;
println!;
Working with JSON-RPC Responses
Responses Serialization
Response serialization is performed on the server side, sending the responses.
use ;
use json;
// Create a successful response
let result = json!;
let response = success;
// Serialize to JSON string
let response_json = to_response_string.unwrap;
println!;
Here's the corresponding valid JSON-RPC 2.0 response example to be sent:
Response Deserialization (parsing)
Response deserialization is performed on the client side, receiving the responses.
This library will deserialize a JSON string message into a Response object that can be processed by your application.
use ;
// Deserialize from JSON string
let parsed_response = from_response_string.unwrap;
println!;
Batch Processing
use ;
// Create a batch with multiple requests
let mut batch = new;
let params1 = Some;
let params2 = Some;
batch.push;
batch.push;
// Serialize batch to JSON
let batch_json = to_batch_string.unwrap;
println!;
// Deserialize batch from JSON
let parsed_batch = from_batch_string.unwrap;
println!;
Supported JSON-RPC Versions
This library supports all major JSON-RPC specifications:
- JSON-RPC 1.0: The original specification with basic request/response format
- JSON-RPC 1.1: Intermediate specification with named parameters and batch support
- JSON-RPC 2.0: Current widely adopted specification with improved error handling and batch processing
Dependencies
- serde, serde_json for JSON serialization
Testing
This library is tested against a set of valid and invalid messages contained in the official JSON-RPC 2.0 and 1.0 specifications and on the Wikipedia page dedicated to JSON-RPC.