如何使用带有不同对象的JSON数组的Serde来获得成功和错误?

时间:2016-06-01 07:01:54

标签: json rust serde

我想使用Serde创建一个包含错误消息的数组以及正确的对象:

extern crate serde; // 1.0.70
#[macro_use]
extern crate serde_derive; // 1.0.70
extern crate serde_json; // 1.0.24

#[derive(Serialize, Deserialize, Debug)]
pub struct MyError {
    error: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MyAge {
    age: i32,
    name: String,
}

fn get_results(ages: Vec<i32>) -> Vec<MyAge> {
    let mut results = vec![];
    for age in ages {
        if age < 100 && age > 0 {
            results.push(MyAge {
                age: age,
                name: String::from("The dude"),
            });
        } else {
            results.push(MyError {
                error: String::from(format!("{} is invalid age", age)),
            });
        }
    }
    results
}

当我传入Vec [1, -6, 7]时,我想序列化为JSON:

[{"age": 1, "name": "The dude"},{"error": "-6 is invalid age"},{"age": 7, "name": "The dude"}]

我该怎么做?知道如何反序列化这样的数组也是不错的。

2 个答案:

答案 0 :(得分:10)

这是一种方法:

#[macro_use]
extern crate serde_derive; // 1.0.70
extern crate serde; // 1.0.70
extern crate serde_json; // 1.0.24

#[derive(Serialize, Deserialize, Debug)]
pub struct MyError {
    error: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MyAge {
    age: i32,
    name: String,
}

#[derive(Debug)]
enum AgeOrError {
    Age(MyAge),
    Error(MyError),
}

impl serde::Serialize for AgeOrError {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        match self {
            &AgeOrError::Age(ref my_age) => serializer.serialize_some(my_age),
            &AgeOrError::Error(ref my_error) => serializer.serialize_some(my_error),
        }
    }
}

enum AgeOrErrorField {
    Age,
    Name,
    Error,
}

impl<'de> serde::Deserialize<'de> for AgeOrErrorField {
    fn deserialize<D>(deserializer: D) -> Result<AgeOrErrorField, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        struct AgeOrErrorFieldVisitor;

        impl<'de> serde::de::Visitor<'de> for AgeOrErrorFieldVisitor {
            type Value = AgeOrErrorField;

            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                write!(formatter, "age or error")
            }

            fn visit_str<E>(self, value: &str) -> Result<AgeOrErrorField, E>
            where
                E: serde::de::Error,
            {
                Ok(match value {
                    "age" => AgeOrErrorField::Age,
                    "name" => AgeOrErrorField::Name,
                    "error" => AgeOrErrorField::Error,
                    _ => panic!("Unexpected field name: {}", value),
                })
            }
        }

        deserializer.deserialize_any(AgeOrErrorFieldVisitor)
    }
}

impl<'de> serde::Deserialize<'de> for AgeOrError {
    fn deserialize<D>(deserializer: D) -> Result<AgeOrError, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        deserializer.deserialize_map(AgeOrErrorVisitor)
    }
}

struct AgeOrErrorVisitor;

impl<'de> serde::de::Visitor<'de> for AgeOrErrorVisitor {
    type Value = AgeOrError;

    fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(formatter, "age or error")
    }

    fn visit_map<A>(self, mut map: A) -> Result<AgeOrError, A::Error>
    where
        A: serde::de::MapAccess<'de>,
    {
        let mut age: Option<i32> = None;
        let mut name: Option<String> = None;
        let mut error: Option<String> = None;
        loop {
            match map.next_key()? {
                Some(AgeOrErrorField::Age) => age = map.next_value()?,
                Some(AgeOrErrorField::Name) => name = map.next_value()?,
                Some(AgeOrErrorField::Error) => error = map.next_value()?,
                None => break,
            }
        }
        if let Some(error) = error {
            Ok(AgeOrError::Error(MyError { error: error }))
        } else {
            Ok(AgeOrError::Age(MyAge {
                age: age.expect("!age"),
                name: name.expect("!name"),
            }))
        }
    }
}

fn get_results(ages: &[i32]) -> Vec<AgeOrError> {
    let mut results = Vec::with_capacity(ages.len());
    for &age in ages.iter() {
        if age < 100 && age > 0 {
            results.push(AgeOrError::Age(MyAge {
                age: age,
                name: String::from("The dude"),
            }));
        } else {
            results.push(AgeOrError::Error(MyError {
                error: format!("{} is invalid age", age),
            }));
        }
    }
    results
}

fn main() {
    let v = get_results(&[1, -6, 7]);
    let serialized = serde_json::to_string(&v).expect("Can't serialize");
    println!("serialized: {}", serialized);
    let deserialized: Vec<AgeOrError> =
        serde_json::from_str(&serialized).expect("Can't deserialize");
    println!("deserialized: {:?}", deserialized);
}

请注意,在反序列化中,我们无法重用自动生成的反序列化器,因为:

  1. 反序列化是将字段流式传输给我们的,我们无法查看到字符串化的JSON表示中并猜测它是什么;
  2. 我们无法访问Serde生成的serde::de::Visitor实现。
  3. 我也做了一个捷径,panic知道了错误。在生产代码中,您需要返回正确的Serde错误。

    另一个解决方案是使所有字段的合并结构可选,如下所示:

    #[macro_use]
    extern crate serde_derive; // 1.0.70
    extern crate serde; // 1.0.70
    extern crate serde_json; // 1.0.24
    
    #[derive(Debug)]
    pub struct MyError {
        error: String,
    }
    
    #[derive(Debug)]
    pub struct MyAge {
        age: i32,
        name: String,
    }
    
    #[derive(Serialize, Deserialize, Debug)]
    pub struct MyAgeOrError {
        #[serde(skip_serializing_if = "Option::is_none")]
        age: Option<i32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        name: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        error: Option<String>,
    }
    
    impl MyAgeOrError {
        fn from_age(age: MyAge) -> MyAgeOrError {
            MyAgeOrError {
                age: Some(age.age),
                name: Some(age.name),
                error: None,
            }
        }
        fn from_error(error: MyError) -> MyAgeOrError {
            MyAgeOrError {
                age: None,
                name: None,
                error: Some(error.error),
            }
        }
    }
    
    fn get_results(ages: &[i32]) -> Vec<MyAgeOrError> {
        let mut results = Vec::with_capacity(ages.len());
        for &age in ages.iter() {
            if age < 100 && age > 0 {
                results.push(MyAgeOrError::from_age(MyAge {
                    age: age,
                    name: String::from("The dude"),
                }));
            } else {
                results.push(MyAgeOrError::from_error(MyError {
                    error: format!("{} is invalid age", age),
                }));
            }
        }
        results
    }
    
    fn main() {
        let v = get_results(&[1, -6, 7]);
        let serialized = serde_json::to_string(&v).expect("Can't serialize");
        println!("serialized: {}", serialized);
        let deserialized: Vec<MyAgeOrError> =
            serde_json::from_str(&serialized).expect("Can't deserialize");
        println!("deserialized: {:?}", deserialized);
    }
    

    我保证这个,因为它允许Rust结构(例如MyAgeOrError)匹配JSON的布局。这样,JSON布局就会在Rust代码中记录下来。

答案 1 :(得分:6)

自版本0.9.6开始,Serde支持internally taggeduntagged enums

以下代码显示了如何通过使用具有属性proj wgs84的枚举来完成此操作的示例。

#[serde(untagged)]

上面的代码输出以下JSON:

#[macro_use]
extern crate serde_derive; // 1.0.70
extern crate serde_json; // 1.0.24

#[derive(Serialize, Deserialize, Debug)]
pub struct MyError {
    error: String,
}

#[derive(Serialize, Deserialize, Debug)]
pub struct MyAge {
    age: i32,
    name: String,
}

#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)]
pub enum AgeOrError {
    Age(MyAge),
    Error(MyError),
}

fn get_results(ages: Vec<i32>) -> Vec<AgeOrError> {
    let mut results = Vec::with_capacity(ages.len());
    for age in ages {
        if age < 100 && age > 0 {
            results.push(AgeOrError::Age(MyAge {
                age: age,
                name: String::from("The dude"),
            }));
        } else {
            results.push(AgeOrError::Error(MyError {
                error: format!("{} is invalid age", age),
            }));
        }
    }
    results
}

fn main() {
    let results = get_results(vec![1, -6, 7]);
    let json = serde_json::to_string(&results).unwrap();
    println!("{}", json);
}

有关Serde枚举表示的更多信息,请参阅overview