JSON sample को serde के लिए तैयार Rust structs में बदलें। हर struct Debug, Clone, Serialize, Deserialize derive करता है; field नाम Rust के snake_case convention का पालन करते हैं और अलग होने पर #[serde(rename)] से मूल key से जुड़े रहते हैं।
यह शुरुआती बिंदु है जब आपको typed reqwest responses चाहिए, axum या actix-web Json<T> extractor के लिए request type चाहिए, या आप log या event JSON parse करने वाला CLI लिख रहे हैं।
टाइप का नाम में top-level struct का नाम डालें (उदाहरण के लिए AccessLog)।.rs फ़ाइल में पेस्ट करें और Cargo.toml में serde (derive feature के साथ) और serde_json जोड़ें।serde_json::from_str::<AccessLog>(&body)? या resp.json::<AccessLog>().await? से इस्तेमाल करें।{
"timestamp": 1714550400,
"level": "warn",
"type": "http_request",
"status": 503,
"latency_ms": 1250.7,
"headers": [{ "name": "user-agent", "value": "curl/8.4" }],
"trace_id": null
}use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AccessLog {
pub timestamp: i64,
pub level: String,
#[serde(rename = "type")]
pub type_: String,
pub status: i64,
pub latency_ms: f64,
pub headers: Vec<Header>,
pub trace_id: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Header {
pub name: String,
pub value: String,
}
type Rust keyword है, इसलिए field #[serde(rename = "type")] के साथ type_ है। Integers आकार चाहे जो हो i64 हैं, और latency_ms f64 है। trace_id हमेशा null ही है, इसलिए उसका type अज्ञात है और वह serde_json::Value रहता है; उसे असली type से बदलें, जैसे Option<String>।
#[derive(Debug, Clone, Serialize, Deserialize)] और pub fields।#[serde(rename = "originalKey")]।#[serde(default, skip_serializing_if = "Option::is_none")] के साथ Option<T>।null वाले fields: बिना attributes के सादा Option<T>।i64, decimals के लिए f64, bool, String, Vec<T>, और मिले-जुले types के लिए serde_json::Value।type, match या ref जैसी keyword keys के अंत में underscore लगता है (type_)।pub type Root = Vec<RootItem>; बनाता है।["status"].as_i64() जैसे serde_json::Value lookups को resp.json::<AccessLog>() से बदलें, ताकि गलत लिखा field compile time पर ही पकड़ा जाए।
Client जो JSON भेजता है उससे struct बनाएं और उसे Json(payload): Json<CreateOrder> के रूप में स्वीकार करें; न मौजूद या गलत type वाले fields वाली requests आपके handler कोड के चलने से पहले ही framework अस्वीकार कर देता है।
एक log line से struct बनाएं, फिर BufReader::lines() की हर line deserialize करें और कुछ ही lines में field-वार aggregation टूल पाएं।
i64 होते हैं, चाहे कितने भी छोटे हों। जहां range पता हो वहां खुद छोटा करें, जैसे u16 status codes या u32 counters।i64 या String रहते हैं। chrono या time types पर जाने के लिए उन crates के serde features चालू करने होंगे।serde_json::Value इस्तेमाल करने वाला आउटपुट पूरा path लिखता है, इसलिए अतिरिक्त use नहीं चाहिए, लेकिन serde_json dependency चाहिए।#[serde(rename_all = "camelCase")] की बजाय field के हिसाब से होते हैं। अगर keys एक ही convention में हैं तो आप उन्हें खुद समेट सकते हैं।"level": "warn" जैसे तय values वाले fields बाद में enum बनाने के अच्छे उम्मीदवार हैं।#[serde(default)] भी क्यों मिलता है?serde पहले से न मौजूद Option को None से भरता है, लेकिन explicit default रखने से बाद में type को Vec<T> जैसा कुछ करने पर भी न मौजूद keys स्वीकार होती रहती हैं। skip_serializing_if JSON वापस लिखते समय None fields छोड़ता है, जिससे मूल आकार बना रहता है।
दोनों Option<T> बनते हैं, लेकिन default और skip_serializing_if attributes सिर्फ़ उन fields को मिलते हैं जिनकी key गायब हो सकती है। हमेशा मौजूद लेकिन कभी-कभी null वाला field वापस null के रूप में लिखा जाता है।
&str fields और 'de lifetime से borrow क्यों नहीं?Zero-copy fields (&'a str) इनपुट buffer से ज़्यादा समय तक नहीं रह सकते, जो आम response handling में असुविधाजनक है। Owned String default है; performance मायने रखे तो ज़्यादा इस्तेमाल होने वाले fields को Cow<'a, str> में बदलें।
Default या PartialEq derives जोड़ सकते हैं?आउटपुट सिर्फ़ Debug, Clone, Serialize और Deserialize derive करता है। जब सभी field types उन्हें समर्थन दें तो derive सूची में और खुद जोड़ें (serde_json::Value PartialEq implement करता है)।