#[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)]// <-- this is a container attribute structS { #[serde(default)]// <-- this is a field attribute f: i32, s_e: E }
#[derive(Serialize, Deserialize, Debug)] #[serde(rename = "e")]// <-- this is also a container attribute enumE { #[serde(rename = "a")]// <-- this is a variant attribute A(String), }
fnmain() { let point_s = S { f: 1 , s_e: E::A(String::from("inner-enum"))}; // Convert the Point to a JSON string. let serialized_a = serde_json::to_string(&point_s).unwrap();
println!("serialized_a: {}", serialized_a);
let point_e = E::A(String::from("my-enum")); // Convert the Point to a JSON string. let serialized_e = serde_json::to_string(&point_e).unwrap();
println!("serialized_e: {}", serialized_e) }
1 2 3 4 5
/Users/caiwenhui/.cargo/bin/cargo run --color=always --package serde_test --bin serde_test Finished dev [unoptimized + debuginfo] target(s) in 0.01s Running `target/debug/serde_test` serialized_a: {"f":1,"s_e":{"a":"inner-enum"}} serialized_e: {"a":"my-enum"}
#[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)]// <-- this is a container attribute #[serde(rename_all = "UPPERCASE")] structS { #[serde(default)]// <-- this is a field attribute f: i32, hello_world: E }
#[derive(Serialize, Deserialize, Debug)] #[serde(rename = "e")]// <-- this is also a container attribute enumE { #[serde(rename = "a")]// <-- this is a variant attribute A(String), }
fnmain() { let point_s = S { f: 1 , hello_world: E::A(String::from("inner-enum"))}; // Convert the Point to a JSON string. let serialized_a = serde_json::to_string(&point_s).unwrap();
// normal output => serialized_a: {"f":1,"hello_world":{"a":"inner-enum"}} println!("serialized_a: {}", serialized_a); }
1 2 3 4
/Users/caiwenhui/.cargo/bin/cargo run --color=always --package serde_test --bin serde_test Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/serde_test` serialized_a: {"F":1,"HELLO_WORLD":{"a":"inner-enum"}}
fnmain() { let s = r#" {"f":1,"s_e":{"a":"inner-enum"}} "#; let uns: S = serde_json::from_str(&s).unwrap();
println!("un: {:?}", uns); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/Users/caiwenhui/.cargo/bin/cargo run --color=always --package serde_test --bin serde_test Finished dev [unoptimized + debuginfo] target(s) in 0.02s Running `target/debug/serde_test` thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("unknown field `s_e`, expected `f` or `s_e`", line: 2, column: 44)', src/main.rs:34:43 stack backtrace: 0: rust_begin_unwind at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/std/src/panicking.rs:495:5 1: core::panicking::panic_fmt at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/panicking.rs:92:14 2: core::option::expect_none_failed at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca/library/core/src/option.rs:1268:5 3: core::result::Result<T,E>::unwrap at /Users/caiwenhui/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/result.rs:973:23 4: serde_test::main at ./src/main.rs:34:18 5: core::ops::function::FnOnce::call_once at /Users/caiwenhui/.rustup/toolchains/stable-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/function.rs:227:5 note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.