【发布时间】:2021-03-13 08:49:54
【问题描述】:
基本上我有以下数据结构:
enum Value {
Scalar(f64),
Vector3((f64, f64, f64)),
}
struct Data {
attribute: Value,
}
使用serde/serde_json 进行序列化会得到以下结果
{
"attribute": { "Scalar": 1.0 }
}
{
"attribute": { "Vector3": [ 1.0, 2.0, 3.0 ] }
}
反序列化按预期工作。但是,是否可以将以下反序列化为相同的数据结构?
{
"attribute": 1.0
}
{
"attribute": [ 1.0, 2.0, 3.0 ]
}
是否可以“映射”f64 到 Scalar(f64) 和 Vec<f64> 到 Vector3((f64, f64, f64))?
这两种形式都应该有效。如果以下最小示例有效,那就太好了:
[dependencies]
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
enum Value {
Scalar(f64),
Vector3((f64, f64, f64)),
}
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Data {
attribute: Value,
}
fn main() {
let js1 = r#"
{
"attribute": { "Scalar": 1.0 }
}"#;
let d1: Data = serde_json::from_str(js1).unwrap();
println!("{:?}", d1);
let js2 = r#"
{
"attribute": { "Vector3": [ 1.0, 2.0, 3.0 ] }
}"#;
let d2: Data = serde_json::from_str(js2).unwrap();
println!("{:?}", d2);
let js3 = r#"
{
"attribute": 1.0
}"#;
let d3: serde_json::Result<Data> = serde_json::from_str(js3);
match d3 {
Ok(d3) => println!("{:?}", d3),
Err(e) => println!("{:?}", e),
}
let js4 = r#"
{
"attribute": [ 1.0, 2.0, 3.0 ]
}"#;
let d4: serde_json::Result<Data> = serde_json::from_str(js4);
match d4 {
Ok(d4) => println!("{:?}", d4),
Err(e) => println!("{:?}", e),
}
}
输出:
Data { attribute: Scalar(1.0) }
Data { attribute: Vector3((1.0, 2.0, 3.0)) }
Error("expected value", line: 3, column: 22)
Error("expected value", line: 3, column: 22)
【问题讨论】:
-
您希望两种表单同时工作吗?还是只是第二种形式?
-
@kmdreko 两种形式。如果最小的例子有效,那就太好了。在我的特殊情况下,
Value包含超过 2 个变体。
标签: rust deserialization serde