日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

在Rust中使用Serde的詳細指南

來源: 責編: 時間:2024-03-26 09:33:11 167觀看
導讀在處理HTTP請求時,我們總是需要在一種數據結構(可以是enum、struct等)和一種可以存儲或傳輸并稍后重建的格式(例如JSON)之間來回轉換。Serde是一個庫(crate),用于高效、通用地序列化和反序列化Rust數據結構。在本文中,將

在處理HTTP請求時,我們總是需要在一種數據結構(可以是enum、struct等)和一種可以存儲或傳輸并稍后重建的格式(例如JSON)之間來回轉換。pXx28資訊網——每日最新資訊28at.com

Serde是一個庫(crate),用于高效、通用地序列化和反序列化Rust數據結構。在本文中,將詳細介紹如何使用Serde對數據結構進行序列化和反序列化操作。pXx28資訊網——每日最新資訊28at.com

讓我們從一個簡單的結構體Student開始,它的定義如下所示,并進行初始化。pXx28資訊網——每日最新資訊28at.com

use serde::{Serialize, Deserialize};#[derive(Debug, Clone, Serialize, Deserialize)]struct Student {    pub name: String,     pub student_id: String,}let student = Student{name:"tom".to_owned(), student_id:"J19990".to_owned()};

pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

約定

對于上面的示例,如果我們使用serde_json::to_string(&student)將其轉換為JSON字符串,那么輸出將如下所示。pXx28資訊網——每日最新資訊28at.com

{  "name": "tom",  "student_id": "J19990"}

pXx28資訊網——每日最新資訊28at.com

看起來太棒了!然而,這依賴于發送HTTP請求的內容,很有可能會與Rust中的數據結構有不同的大小寫約定。pXx28資訊網——每日最新資訊28at.com

基本上有兩種方法可以進行約定:可以重命名字段;也可以對整個結構應用大小寫約定。pXx28資訊網——每日最新資訊28at.com

例如,我們實際上希望使用studentId而不是student_id作為字段名。pXx28資訊網——每日最新資訊28at.com

方法1:使用#[serde(rename="")重命名單個字段。pXx28資訊網——每日最新資訊28at.com

struct Student {    pub name: String,     #[serde(rename="studentId")    pub student_id: String,}

pXx28資訊網——每日最新資訊28at.com

方法2:使用#[serde(rename_all="camelCase")將大小寫約定駝峰形式,應用于整個結構體。pXx28資訊網——每日最新資訊28at.com

#[serde(rename_all = "camelCase")]struct Student {    pub name: String,     pub student_id: String,}

pXx28資訊網——每日最新資訊28at.com

任何一種方法都會給出以下輸出:pXx28資訊網——每日最新資訊28at.com

{  "name": "tom",  "studentId": "J19990"}

pXx28資訊網——每日最新資訊28at.com

除了camelCase之外,還可以應用其他的約定。取值為“lowercase, UPPERCASE, PascalCase, camelCase, snake_case, SCREAMING_SNAKE_CASE, kebab-case, SCREAMING-KEBAB-CASE”。pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

Skip

Skip可用于不希望序列化或反序列化的字段。下面是一個簡單的例子。讓我們給Student添加birth_year和age。pXx28資訊網——每日最新資訊28at.com

struct Student {    pub name: String,     pub student_id: String,    pub birth_year: u32,    pub age: u32,}

pXx28資訊網——每日最新資訊28at.com

我們可能希望動態更新年齡,因此需要對學生birth_year的引用。但是,當我們發送請求時,應該只顯示age字段,這可以使用#[serde(skip)]來解決。pXx28資訊網——每日最新資訊28at.com

struct Student {    pub name: String,     pub student_id: String,    #[serde(skip)]    pub birth_year: u32,    pub age: u32,}

pXx28資訊網——每日最新資訊28at.com

通過這樣做,我們的JSON對象將變成:pXx28資訊網——每日最新資訊28at.com

{  "name": "tom",  "studentId": "J19990",  "age": 123}

pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

Skip If

最常見的兩種使用方法是作用于Option字段和空的vector字段。pXx28資訊網——每日最新資訊28at.com

Option

假設我們有一個middle_name: Option<String>字段,如果我們想在學生沒有這個字段的情況下跳過這個字段,我們可以這樣做。pXx28資訊網——每日最新資訊28at.com

struct Student {    pub name: String,     pub student_id: String,    #[serde(skip_serializing_if="Option::is_none")]    pub middle_name: Option<String>}

pXx28資訊網——每日最新資訊28at.com

這將為帶有或不帶有中間名的學生生成如下JSON:pXx28資訊網——每日最新資訊28at.com

// 沒有中間名{  "name": "tom",  "studentId": "J19990",}// 有中間名{  "name": "tom",  "studentId": "J19990",  "middleName": "middle"}

pXx28資訊網——每日最新資訊28at.com

Vector

例如,我們為student結構體提供了pets: Vec<String>字段。由于學生不必擁有寵物,它可以是一個空向量。pXx28資訊網——每日最新資訊28at.com

要跳過對空向量的序列化,可以向字段添加以下屬性。pXx28資訊網——每日最新資訊28at.com

#[serde(skip_serializing_if="Vec::is_empty")]pub pets: Vec<String>,

pXx28資訊網——每日最新資訊28at.com

有屬性和沒有屬性之間的輸出差異如下所示。pXx28資訊網——每日最新資訊28at.com

// 沒有屬性{  "name": "tom",  "studentId": "J19990",  "pets": []}// 有屬性{  "name": "tom",  "studentId": "J19990"}

pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

Flatten

讓我們創建一個名為SideInfo的新結構體,并將Student結構體更改為以下內容。pXx28資訊網——每日最新資訊28at.com

#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(rename_all = "camelCase")]struct Student {    pub name: String,     pub student_id: String,    #[serde(skip_serializing_if="Option::is_none")]    pub side_info: Option<SideInfo>}#[derive(Debug, Clone, Serialize, Deserialize, Default)]#[serde(rename_all = "camelCase")]struct SideInfo {    #[serde(skip_serializing_if="Option::is_none")]    pub pets: Option<Vec<String>>,    #[serde(skip_serializing_if="Option::is_none")]    pub address: Option<String>,}

pXx28資訊網——每日最新資訊28at.com

讓我們創建一個新的StudentpXx28資訊網——每日最新資訊28at.com

let student = Student{name:"dan".to_owned(), student_id: "1".to_owned(), side_info:Some(SideInfo{address:Some("47 street".to_owned()), ..Default::default()})};

pXx28資訊網——每日最新資訊28at.com

并輸出它的JSON字符串:pXx28資訊網——每日最新資訊28at.com

{  "name": "dan",  "studentId": "1",  "sideInfo": {    "address": "47 street"  }}

pXx28資訊網——每日最新資訊28at.com

如你所見,地址字段嵌套在sideInfo中。我們可以通過將屬性flatten添加到Student結構體中的sideInfo字段上,來消除嵌套。pXx28資訊網——每日最新資訊28at.com

#[serde(skip_serializing_if="Option::is_none", flatten)]pub side_info: Option<SideInfo>

pXx28資訊網——每日最新資訊28at.com

就會變成:pXx28資訊網——每日最新資訊28at.com

{  "name": "dan",  "studentId": "1",  "address": "47 street"}

pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

枚舉上的標簽與非標簽

假設我們有一個StudentList enum,如下所示:pXx28資訊網——每日最新資訊28at.com

enum StudentList {    Student1(Student),     Student2(Student)}

pXx28資訊網——每日最新資訊28at.com

定義學生名單pXx28資訊網——每日最新資訊28at.com

let student1 = Student{name:"tom".to_owned(), student_id:"J19990".to_owned(), pets: vec![], middle_name:Some("middle".to_owned())};let student2 = Student{name:"dan".to_owned(), student_id:"J19990".to_owned(), pets: vec![], middle_name:Some("middle".to_owned())};let student_list = vec![StudentList::Student1(student1), StudentList::Student2(student2)];

pXx28資訊網——每日最新資訊28at.com

如果我們像現在一樣打印出JSON,它將如下所示,它是有標簽的,是serde的默認行為。pXx28資訊網——每日最新資訊28at.com

[  {    "Student1": {      "name": "tom",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  },  {    "Student2": {      "name": "dan",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  }]

pXx28資訊網——每日最新資訊28at.com

如果你希望所有標簽都具有相同的名稱,例如Student,該怎么辦呢?你可能認為可以使用rename_all來實現這一點,但實際上并非如此,應該手動重命名枚舉中的每個變體。pXx28資訊網——每日最新資訊28at.com

#[derive(Debug, Clone, Serialize, Deserialize)]enum StudentList {    #[serde(rename="Student")]    Student1(Student),     #[serde(rename="Student")]    Student2(Student)}

pXx28資訊網——每日最新資訊28at.com

輸出如下:pXx28資訊網——每日最新資訊28at.com

[  {    "Student": {      "name": "tom",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  },  {    "Student": {      "name": "dan",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  }]

pXx28資訊網——每日最新資訊28at.com

不加標簽

如果我們只想要一個簡單的學生數組,而不顯示枚舉變量名稱,該怎么辦?我們可以通過向枚舉中添加#[serde(untagged)]屬性來實現這一點。通過這樣做,我們的輸出將變成:pXx28資訊網——每日最新資訊28at.com

[  {    "name": "tom",    "studentId": "J19990",    "pets": [],    "middleName": "middle"  },  {    "name": "dan",    "studentId": "J19990",    "pets": [],    "middleName": "middle"  }]

pXx28資訊網——每日最新資訊28at.com

內部標簽

枚舉的另一種表示形式是內部標簽,讓我們創建一個包含不同學生類型的新枚舉,我們將有班長、副班長和普通學生。pXx28資訊網——每日最新資訊28at.com

#[derive(Debug, Clone, Serialize, Deserialize)]#[serde(tag = "type", rename_all="camelCase")]enum StudentType {    Regular(Student),     Leader(Student),     SubLeader(Student)}

pXx28資訊網——每日最新資訊28at.com

指定serde(tag = "type")將允許我們在內容中使用標簽來識別我們正在處理的變體,如下所示:pXx28資訊網——每日最新資訊28at.com

[  {    "type": "leader",    "name": "tom",    "studentId": "J19990",    "pets": [],    "middleName": "middle"  },  {    "type": "regular",    "name": "dan",    "studentId": "J19990",    "pets": [],    "middleName": "middle"  }]

pXx28資訊網——每日最新資訊28at.com

相鄰標簽

表示標簽和內容作為同一對象中的兩個字段彼此相鄰。將枚舉的屬性修改如下:pXx28資訊網——每日最新資訊28at.com

#[serde(tag = "type", content = "student", rename_all="camelCase")]

pXx28資訊網——每日最新資訊28at.com

json數據變成:pXx28資訊網——每日最新資訊28at.com

[  {    "type": "leader",    "student": {      "name": "tom",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  },  {    "type": "regular",    "student": {      "name": "dan",      "studentId": "J19990",      "pets": [],      "middleName": "middle"    }  }]

pXx28資訊網——每日最新資訊28at.com

可以用Serde做很多很多事情。pXx28資訊網——每日最新資訊28at.com

pXx28資訊網——每日最新資訊28at.com

本文鏈接:http://www.www897cc.com/showinfo-26-79291-0.html在Rust中使用Serde的詳細指南

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: LG Innotek 宣布進軍半導體玻璃基板業務

下一篇: 一圖看懂常見 AWS 網絡架構圖

標簽:
  • 熱門焦點
Top 主站蜘蛛池模板: 嘉峪关市| 乳山市| 赤峰市| 东丰县| 巴南区| 滁州市| 元朗区| 分宜县| 台江县| 磐石市| 晋城| 潜江市| 新宁县| 长顺县| 建德市| 嘉兴市| 葫芦岛市| 永安市| 长丰县| 隆化县| 鄂托克旗| 如皋市| 五河县| 噶尔县| 山阳县| 泊头市| 牙克石市| 张北县| 准格尔旗| 外汇| 富源县| 清远市| 闽清县| 壶关县| 松溪县| 黑龙江省| 德令哈市| 莱芜市| 龙川县| 合作市| 大同县|