You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I want to do as shown in below example, but it doesn't seem to work. I feel like there is something I'm missing or serde is missing.
I'd solve this by making two enums, one that is the real enum, and one that is only used (and seen) in a custom deserialize. But I don't like that pattern, so I want to know if there's something that can be improved here.
// [dependencies]// serde = {version = "1", features = ["derive"]}// serde_json = "1"use serde;use serde_json;#[derive(serde::Deserialize)]pubstructFoo{pubbar:i64,}#[derive(serde::Deserialize,Default)]pubstructNoContent{}#[derive(serde::Deserialize)]#[serde(tag = "tag", content = "content")]#[serde(rename_all = "snake_case")]pubenumData{WithContent(Foo),WithOptContent(Option<NoContent>),// There is no way to make this work.WithoutContent(#[serde(default)]NoContent),}#[test]fndeserialize(){let things = vec![r#"{"tag":"with_content", "content": { "bar": 10}}"#, // This works finer#"{"tag":"with_opt_content"}"#, // This works because the content is wrapped with an Optionr#"{"tag":"without_content" }"#, // This fails];for i in things {
serde_json::from_str::<Data>(i).unwrap_or_else(|_| panic!("failed on {:?}", i));}}
The text was updated successfully, but these errors were encountered:
I want to do as shown in below example, but it doesn't seem to work. I feel like there is something I'm missing or serde is missing.
I'd solve this by making two enums, one that is the real enum, and one that is only used (and seen) in a custom deserialize. But I don't like that pattern, so I want to know if there's something that can be improved here.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=2bf7a3b1c76f9fe9d7741dce671011cf
The text was updated successfully, but these errors were encountered: