jrpc-types
This crate implements the structures as defined in the JSON-RPC 2.0 Specification
Requirements
These requirements derived from the specification itself.
- The "jsonrpc" field MUST be present in requests, responses, and notifications. Futhormore, the value of the "jsonrpc" field MUST be "2.0".
- The "id" field MUST contain a String, Number, or Null value in any request or response.
- The Null value for "id" in a request object is discouraged, as it's used for responses in which the original request id is unknown.
- The Number value for "id" SHOULD NOT contain fractional parts, as many decimal fractions cannot be represented exactly as binary fraction.
- The "method" field MUST contain a String in any request.
- The optional "params" field MUST be either an Object (by-name) or Array (by-position) IF the "params" are present in the request or notification.
Dependencies
- serde: used for (de)serialization
- serde_json: used for JSON de(serialization) implementation
- thiserror: used for error reporting
Usage
Requests
You can easily build a JSON-RPC request:
use JsonRpcRequest;
let data = vec!;
let req = builder
.method
.params
.unwrap // Serialization of parameters could fail, so you need to catch this.
.id
.build;
Deserializing a request from a string is super easy:
use JsonRpcRequest;
let data = r#"{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}"#;
match try_into
Notifications
JSON-RPC Notifications are pretty much Requests, without an ID... You can build Notifications like:
use JsonRpcNotification;
let data = vec!;
let req = builder
.method
.params
.unwrap // Serialization of parameters could fail, so you need to catch this.
.build;
Deserializing a notification from a string is super easy:
use JsonRpcNotification;
let data = r#"{"jsonrpc": "2.0", "method": "update", "params": [1,2,3,4,5]}"#;
match try_into
Response
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 JsonRpcResponse;
let data = vec!;
let req = builder
.success
.result
.unwrap // Serialization of parameters could fail, so you need to catch this.
.id
.build;
Invalid request error:
use JsonRpcResponse;
let data = vec!;
let req = builder
.error
.invalid_request
.id
.build;
Custom Error:
use JsonRpcResponse;
let data = vec!;
let req = builder
.error
.code
.message
.id
.build;
NOTE: If you are processing a JsonRpcRequest and building a JsonRpcResponse, you can use &JsonRpcRequest in the id() builder function.
use ;
// Just for docs, creating this here.. assume you have this.
let req = builder
.id
.method
.params_str
.unwrap // Serialization of parameters could fail, so you need to catch this.
.build;
// ... process stuff ...
let rsp = builder
.id
.success
.result
.unwrap // Serialization of parameters could fail, so you need to catch this.
.build;