-
Notifications
You must be signed in to change notification settings - Fork 177
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
use serde_json::Value
for Client::request()
#477
Comments
Can you elaborate what you mean with a reference to a code snippet? I guess you are talking about the I don't get the benefit of using |
e.g #[derive(Serialize, Deserialize)]
struct SomeStruct {
a: u8,
b: u32,
}
let rpc_client = ...;
let some_instance = SomeStruct { a: 2, b: 534 };
rpc_client.request("send-some-struct", some_instance); that'd be best and easiest to use if another way would be #[derive(Serialize, Deserialize)]
struct SomeStruct {
a: u8,
b: u32,
}
let rpc_client = ...;
let some_instance = SomeStruct { a: 2, b: 534 };
rpc_client.request("send-some-struct", serde_json::to_value(some_instance).unwrap()); but that involves some boilerplate. At the moment request() accepts |
Ok, I see. The intention is that you shouldn't use Thus, you need to define a trait and then you don't have to do the serialize/deserialize stuff it's generated for you Sure, your idea could work but the nothing that prevents that from passing in a String or some invalid JSON-RPC param type. |
Ah, I'd like to avoid using any macros, they always introduce a lot of problems. |
Closing because this is not priority from our side as the main use case is to use However, we have now introduced let x = 0_usize;
let y = "hello";
client.request("my_method", rpc_params! [x, y]); |
+1 If I have time I'll open up a PR that adds support for a editAfter thinking about this for a minute. To clarify is the idea you would do the following?
|
you mean an API like: client.request_json(json!({"jsonrpc":"2.0,"method": "foo","id":1})).await.unwrap() I guess that is okay but would return errors at runtime for invalid JSON-RPC calls instead.
#[rpc(client)]
pub trait SomeNonRustJsonRpc {
#[method(name = "foo")]
fn echo(&self, val: String) -> RpcResult<String>;
} That would generate some code under the hood that serialized/converting the params to #[rpc(client)]
pub trait SomeNonRustJsonRpc {
#[method(name = "foo")]
fn echo(&self, val: String) -> RpcResult<String>;
}
async fn main() {
let client = WsClientBuilder::default().build("ws://myurl.com:80).await?;
let echo = client.echo("lo".to_string().await.unwrap();
} |
Related to that we just merged #864, So you should be able to: let json = json!({"p": 1});
let params = ArrayParams::new();
params.insert(&json).unwrap();
rpc.request("method", params).await.unwrap(); but we could probably impl let json = json!({"p": 1});
rpc.request("method", json.try_into().unwrap()).await.unwrap(); //cc @lexnv thoughts? |
@niklasad1 Thanks for the response! I ended up using the proc_macro which works great. I think for situations where someone can't use the proc macros but still wants to use the client the PR you posted is pretty ergonomic. A |
It'd be much easier to use
serde_json::Value
and serialize structs usingserde_json::to_value
The text was updated successfully, but these errors were encountered: