Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serde practice #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions 6_serde/serde_practice/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "serde_practice"
version = "0.1.0"
edition = "2021"

[dependencies]
chrono = { version = "0.4.38", features = ["serde"] }
humantime-serde = "1.1.1"
serde = { version = "1.0.214", features = ["derive"]}
serde_json = "1.0.132"
serde_yaml = "0.9.34"
toml = "0.8.19"
url = { version = "2.5.3", features = ["serde"] }
uuid = { version = "1.11.0", features = ["serde"] }
36 changes: 36 additions & 0 deletions 6_serde/serde_practice/request.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"type": "success",
"stream": {
"user_id": "8d234120-0bda-49b2-b7e0-fbd3912f6cbf",
"is_private": false,
"settings": 45345,
"shard_url": "https://n3.example.com/sapi",
"public_tariff": {
"id": 1,
"price": 100,
"duration": "1h",
"description": "test public tariff"
},
"private_tariff": {
"client_price": 250,
"duration": "1m",
"description": "test private tariff"
}
},
"gifts": [
{
"id": 1,
"price": 2,
"description": "Gift 1"
},
{
"id": 2,
"price": 3,
"description": "Gift 2"
}
],
"debug": {
"duration": "234ms",
"at": "2019-06-28T08:35:46+00:00"
}
}
39 changes: 39 additions & 0 deletions 6_serde/serde_practice/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use serde::{Serializer, Deserializer};
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct Event {
name: String,
#[serde(serialize_with = "serialize_date", deserialize_with = "deserialize_date")]
date: String,
}

// Функція серіалізації дати
fn serialize_date<S>(date: &str, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&format!("Date: {}", date))
}

// Функція десеріалізації дати
fn deserialize_date<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
Ok(s.replace("Date: ", ""))
}

fn main() {
let event = Event {
name: "Концерт".to_string(),
date: "2024-11-15".to_string(),
};

let json = serde_json::to_string(&event).expect("Помилка серіалізації");
println!("Серіалізований JSON з кастомною датою: {}", json);

let deserialized_event: Event = serde_json::from_str(&json).expect("Помилка десеріалізації");
println!("Десеріалізована подія: {:?}", deserialized_event);
}