Skip to content

Commit

Permalink
serde_json1: add enum1
Browse files Browse the repository at this point in the history
  • Loading branch information
bouzuya committed Jul 20, 2022
1 parent 6c4392d commit e0cc63d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
33 changes: 33 additions & 0 deletions serde_json1/src/enum1.rs
@@ -0,0 +1,33 @@
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
struct Z {
n: i32,
}

#[derive(Clone, Debug, Eq, Deserialize, PartialEq, Serialize)]
#[serde(tag = "type")]
enum X {
Y { s: String },
Z(Z),
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test() -> anyhow::Result<()> {
assert_eq!(
serde_json::to_string(&X::Y {
s: "123".to_string(),
})?,
r#"{"type":"Y","s":"123"}"#
);
assert_eq!(
serde_json::to_string(&X::Z(Z { n: 123 }))?,
r#"{"type":"Z","n":123}"#
);
Ok(())
}
}
1 change: 1 addition & 0 deletions serde_json1/src/main.rs
@@ -1,4 +1,5 @@
mod bool_from_01;
mod enum1;

fn main() {
println!("Hello, world!");
Expand Down

0 comments on commit e0cc63d

Please sign in to comment.