简体   繁体   English

如何在 Rust(使用 serde)中反序列化可选 json 参数,可以是字符串或字符串数组

[英]How to deserialize in Rust (using serde) optional json parameter that can be either string or string array

I am new to Rust and I am trying to deserialize JSON data using serde library.我是 Rust 的新手,我正在尝试使用 serde 库反序列化 JSON 数据。 I have following JSON structure:我有以下 JSON 结构:

{
    “foo”: “bar”,
    “speech”: “something”
}

or或者

{
    “foo”: “bar”,
    “speech”: [“something”, “something else”]
}

or或者

{
    “foo”: “bar”,
}

Ie speech is optional and it can be either string or array of strings.语音是可选的,它可以是字符串或字符串数组。

I can handle deserializing string/array of string using following approach:我可以使用以下方法处理反序列化字符串/字符串数组:

#[derive(Debug, Serialize, Deserialize)]
   struct foo {
   pub foo: String,
   #[serde(deserialize_with = "deserialize_message_speech")]
   speech: Vec<String>
}

I can also handle deserializing optional string/string array attribute using approach:我还可以使用以下方法处理反序列化可选字符串/字符串数组属性:

#[derive(Debug, Serialize, Deserialize)]
struct foo {
   pub foo: String,
   #[serde(skip_serializing_if = "Option::is_none")]
   speech: Option<Vec<String>>
}

or或者

struct foo {
   pub foo: String,
   #[serde(skip_serializing_if = "Option::is_none")]
   speech: Option<String>
}

But combining it all together simply does not work.但是将它们组合在一起根本行不通。 It seems deserialize_with does not work properly with Option type.似乎deserialize_with不适用于Option类型。 Can somebody advice most straightforward and trivial way how to implement this (serde can be pretty complex, I have seen some crazy stuff:) )?有人可以建议最直接和最简单的方法来实现这个(serde 可能非常复杂,我见过一些疯狂的东西:))?

Try using an Enum type for the speech field:尝试对speech字段使用 Enum 类型:

#[derive(Serialize, Deserialize)]
#[serde(untagged)]
enum Speech {
    Str(String),
    StrArray(Vec<String>),   
}

#[derive(Debug, Serialize, Deserialize)]
   struct foo {
   pub foo: String,
   speech: Option<Speech>,
}

Enum is the go-to way to represent a variant type in Rust.枚举是在 Rust 中表示变体类型的首选方式。 See https://serde.rs/enum-representations.html for more detail.有关详细信息,请参阅https://serde.rs/enum-representations.html

with #[serde(untagged)] it works!#[serde(untagged)] 一起工作!

use serde_json;
use std::result::Result;
use std::error::Error;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Speech {
    Str(String),
    StrArray(Vec<String>),   
}

#[derive(Debug, Serialize, Deserialize)]
struct Foo {
   pub foo: String,
   #[serde(skip_serializing_if = "Option::is_none")]
   speech: Option<Speech>,
} 

fn main() -> Result<(), Box<dyn Error>> {

    let json1 = r#"
    {
        "foo": "bar",
        "speech": "something"
    }
    "#;    

    let json2 = r#"
    {
        "foo": "bar",
        "speech": ["something", "something else"]
    }
    "#;    


    let json3 = r#"
    {
        "foo": "bar"
    }
    "#;    

    let foo1: Foo = serde_json::from_str(json1)?;
    let back_to_str_foo1 = serde_json::to_string(&foo1).unwrap();
    println!("foo1 {:#?}", foo1);
    println!("back_to_str_foo1 {}", back_to_str_foo1);

    let foo2: Foo = serde_json::from_str(json2)?;
    let back_to_str_foo2 = serde_json::to_string(&foo2).unwrap();
    println!("foo1 {:#?}", foo2);
    println!("back_to_str_foo2 {}", back_to_str_foo2);


    let foo3: Foo = serde_json::from_str(json3)?;
    let back_to_str_foo3 = serde_json::to_string(&foo3).unwrap();
    println!("foo1 {:#?}", foo3);
    println!("back_to_str_foo3 {}", back_to_str_foo3);

    Ok(())
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM