使用"Serde"进行反序列化时,我能否映射类型?

3
基本上,我有以下数据结构:
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' 映射到 '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)

1
你想让两个表格同时工作吗?还是只需要第二个表格? - kmdreko
@kmdreko 两种形式。如果最简单的例子能够运行就太好了。在我的特定情况下,“Value”包含超过2个变体。 - Rabbid76
2个回答

1
您可以在枚举上使用#[serde(untagged)],使其能够被“映射”。目前Serde不支持同时使用这两种方式,但是有一个hack方法可以实现,尽管我没有尝试过。您还可以在变体内部使用带标记的名称的枚举。
示例:
#[serde(untagged)]
enum Value {
    Scalar(f64),
    ScalarTagged {Scalar: f64},
    Vector3((f64, f64, f64)),
    Vector3Tagged {
        Vector3: (f64, f64, f64)
    }
}

这个通过了您的最小示例


你的想法激发了我的回答。 - Rabbid76

1

@Raspberry1111提供了一个受欢迎的解决方法。然而,更直接的解决方案仍然受到欢迎。


我不得不使用#[serde(untagged)](请参见枚举表示),并更改数据结构以满足我的所有需求:
#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
enum TaggedValue {
    Scalar(f64),
    Vector3((f64, f64, f64)),
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
#[serde(untagged)]
enum Value {
    Scalar(f64),
    Vector3((f64, f64, f64)),
    Tagged(TaggedValue),
}

#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
struct Data {
    attribute: Value,
}

// [...]

输出:

Data { attribute: Tagged(Scalar(1.0)) }
Data { attribute: Tagged(Vector3((1.0, 2.0, 3.0))) }
Data { attribute: Scalar(1.0) }
Data { attribute: Vector3((1.0, 2.0, 3.0)) }

任何更直接的解决方案仍然受欢迎。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接