我有一个使用枚举的结构,但是在打印时它给出了枚举名称和值,而不仅仅是值。我想使用 serde_json 序列化它以作为 JSON 请求发送。
我想将结构重新用于 geth json-rpc 的不同命令,而不是为每种类型的命令制作不同的结构。这就是为什么我想到使用枚举。但我做错了什么。可能是打印,但 json-rpc 表示该参数也无效。
use serde::{Deserialize, Serialize};
#[derive(Debug, Serialize, Deserialize)]
enum Params {
String (String),
Boolean (bool)
}
#[derive(Debug, Serialize, Deserialize)]
struct EthRequest {
jsonrpc : String,
method: String,
params: [Params; 2],
id: i32
}
fn main() {
let new_eth_request = EthRequest {
jsonrpc : "2.0".to_string(),
method : "eth_getBlockByNumber".to_string(),
params : [Params::String("latest".to_string()), Params::Boolean(true)],
id : 1
};
println!("{:#?}", new_eth_request);
}
输出:
EthRequest {
jsonrpc: "2.0",
method: "eth_getBlockByNumber",
params: [
String(
"latest",
),
Boolean(
true,
),
],
id: 1,
}
我需要的是 params 字段params: ["latest",true]