Trait jsonrpsee_core::traits::ToRpcParams
source · pub trait ToRpcParams {
// Required method
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, Error>;
}
Expand description
Marker trait for types that can be serialized as JSON compatible strings.
This trait ensures the correctness of the RPC parameters.
§Note
Please consider using the crate::params::ArrayParams
and crate::params::ObjectParams
than
implementing this trait.
§Examples
§Implementation for hard-coded strings
use jsonrpsee_core::traits::ToRpcParams;
use serde_json::value::RawValue;
struct ManualParam;
impl ToRpcParams for ManualParam {
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error> {
// Manually define a valid JSONRPC parameter.
RawValue::from_string("[1, \"2\", 3]".to_string()).map(Some)
}
}
§Implementation for JSON serializable structures
use jsonrpsee_core::traits::ToRpcParams;
use serde_json::value::RawValue;
use serde::Serialize;
#[derive(Serialize)]
struct SerParam {
param_1: u8,
param_2: String,
};
impl ToRpcParams for SerParam {
fn to_rpc_params(self) -> Result<Option<Box<RawValue>>, serde_json::Error> {
let s = String::from_utf8(serde_json::to_vec(&self)?).expect("Valid UTF8 format");
RawValue::from_string(s).map(Some)
}
}