rs-basic/members/serde-json-demo/src/main.rs

77 lines
1.8 KiB
Rust
Raw Normal View History

2024-02-16 15:57:00 +01:00
#![allow(clippy::disallowed_names)]
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
struct Foo {
name: String,
q: Qux,
t: chrono::DateTime<chrono::Utc>,
}
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
struct Qux {
i: i64,
b: bool,
j: serde_json::Value,
}
2024-02-01 10:39:58 +01:00
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
#[serde(rename_all = "lowercase")]
enum Color {
Green,
Yellow,
Red,
2024-06-25 12:03:55 +02:00
Custom(String),
2024-02-01 10:39:58 +01:00
}
2024-02-19 15:39:41 +01:00
use serde_repr::{Deserialize_repr, Serialize_repr};
2024-02-19 15:37:50 +01:00
#[derive(Serialize_repr, Deserialize_repr, Clone, Debug, PartialEq)]
#[repr(u8)]
enum CLike {
Amida = 1,
Yamda = 2,
Omaba = 10,
}
2024-02-01 10:39:58 +01:00
fn main() -> anyhow::Result<()> {
let qux_source = r#"{
"some Key": [1, 2, 3],
"deep": {
"m": true
}
}"#;
let foo = Foo {
name: String::from("foo"),
q: Qux {
i: 19,
b: false,
2024-02-01 10:39:58 +01:00
j: serde_json::from_str(qux_source).unwrap(),
},
t: chrono::offset::Utc::now(),
};
let foostr = serde_json::to_string(&foo).unwrap();
println!("foo:\n\n{}", foostr);
let foo2: Foo = serde_json::from_str(&foostr).unwrap();
println!("same?: {}", foo == foo2);
2024-02-01 10:39:58 +01:00
let color_source = r#""yellow""#;
let color: Color = serde_json::from_str(color_source)?;
dbg!(&color);
let color_str = serde_json::to_string(&color)?;
dbg!(&color_str);
2024-02-19 15:37:50 +01:00
2024-06-24 13:35:35 +02:00
let custom_color = Color::Custom("Morange".to_string());
dbg!(serde_json::to_string(&custom_color).unwrap());
2024-02-19 15:37:50 +01:00
let cl = CLike::Omaba;
let clrepr = serde_json::to_string(&cl)?;
dbg!(&clrepr);
let cl_from = serde_json::from_str(&clrepr)?;
dbg!(cl == cl_from);
2024-02-01 10:39:58 +01:00
Ok(())
}