天天看點

Rust 中的序列化和反序列化

序列化: 将資料結構或對象轉換成二進制序列的過程。

反序列化:将在序列化過程中所生成的二進制序列轉換成資料結構或者對象的過程。

Serde 是對 Rust 資料結構進行序列化和反序列化一種常用架構。

Serde 中的 trait

Serde 定義了 4 種 trait:

Deserialize A data structure that can be deserialized from any data format supported by Serde.

Deserializer A data format that can deserialize any data structure supported by Serde.

Serialize A data structure that can be serialized into any data format supported by Serde.

Serializer A data format that can serialize any data structure supported by Serde.

Serialize 和 Deserialize 的定義:

若要資料類型支援序列化和反序列化,則該類型需要實作 Serialize 和 Deserialize trait。Serde 提供了 Rust 基礎類型和标準庫類型的 Serialize 和 Deserialize 實作。

對于自定義類型,我們可以自行實作 Serialize 和 Deserialize trait。另外,Serde 提供一個宏 serde_derive 來自動為結構體類型和枚舉類型生成 Serialize 和 Deserialize 實作。該特性需要 Rust 編譯器版本在 1.31及以上,并且在 Cargo.toml 檔案中配置 Serde 依賴時,需要使用 features 指定該特性。例如:

然後就可以在代碼中引入并使用了,例如:

Serializer 和 Deserializer 的實作由第三方 crate 提供,例如:serde_json, serde_yaml 和 bincode。

社群實作的部分資料格式:

JSON, the ubiquitous JavaScript Object Notation used - by many HTTP APIs.

Bincode, a compact binary format used for IPC within - the Servo rendering engine.

CBOR, a Concise Binary Object Representation designed - for small message size without the need for version - negotiation.

YAML, a self-proclaimed human-friendly configuration - language that ain't markup language.

MessagePack, an efficient binary format that resembles - a compact JSON.

TOML, a minimal configuration format used by Cargo.

Pickle, a format common in the Python world.

RON, a Rusty Object Notation.

BSON, the data storage and network transfer format - used by MongoDB.

Avro, a binary format used within Apache Hadoop, with - support for schema definition.

JSON5, A superset of JSON including some productions - from ES5.

Postcard, a no_std and embedded-systems friendly - compact binary format.

URL query strings, in the x-www-form-urlencoded format.

Envy, a way to deserialize environment variables into - Rust structs. (deserialization only)

Envy Store, a way to deserialize AWS Parameter Store - parameters into Rust structs. (deserialization only)

S-expressions, the textual representation of code and - data used by the Lisp language family.

D-Bus's binary wire format.

FlexBuffers, the schemaless cousin of Google's - FlatBuffers zero-copy serialization format.

DynamoDB Items, the format used by rusoto_dynamodb to - transfer data to and from DynamoDB.

Serde JSON

在 Cargo.toml 檔案中添加依賴:

serde_json Crate 提供了 serde_json::to_string、serde_json::to_vec 和 serde_json::to_writer 三個函數将 Rust 資料類型轉為 JSON String、Vec<u8> 和 io::Write(比如檔案或者 TCP 流)。

示例:

serde_json Crate 提供了 serde_json::from_str、serde_json::from_slice 和 serde_json::from_reader 三個函數将字元串、位元組切片(&[u8])和 IO 輸入(檔案或者 TCP 流)解析為對應的 Rust 資料類型。

如果一個 JSON 格式資料沒有對應的 Rust 資料類型,那麼可以将其解析為 serde_json::Value 類型。這是 serde_json Crate 提供的一個遞歸的枚舉類型,它可以表示任何有效的 JSON 資料,其定義如下:

将 JSON 格式資料解析為 serde_json::Value 類型,同樣使用的是:serde_json::from_str、serde_json::from_slice 和 serde_json::from_reader 三個函數。這三個函數傳回值是泛型的,其傳回值類型由指定給變量的類型決定。

RON

RON(Rusty Object Notation) 是一種與 Rust 文法類似的一種序列化格式,它支援所有的 Serde 資料模型。

RON 示例:

RON 格式特點:

使用 (..) 表示結構體,使用 {..} 表示 Map,使用 [..] 表示數組。在使用 JSON 時是不能區分結構體和 Map 的。

可以添加注釋,在 JSON 中不能添加注釋。

與 Rust 文法一樣,最後一個分項也加逗号。

與 serde_json Crate 類似,ron Crate 也使用遞歸枚舉類型來表示 RON 格式資料。這個枚舉類型是:ron::value::Value,它的定義如下:

ron Crate 提供的序列化函數:

ron::ser::to_string: Serializes value and returns it as string.

ron::ser::to_string_pretty: Serializes value in the recommended RON layout in a pretty way.

ron::ser::to_writer: Serializes value into writer.

ron::ser::to_writer_pretty: Serializes value into writer in a pretty way.

ron Crate 提供的反序列化函數:

ron::de::from_bytes: Building a deserializer and deserializing a value of type T from bytes.

ron::de::from_str: Reading data from a reader and feeding into a deserializer.

ron::de::from_reader: Building a deserializer and deserializing a value of type T from a string.

BSON

BSON(Binary JSON) 是一種二進制形式的類似 JSON 的格式(文檔)。

BSON 示例:

很多不同的資料類型可以表示為 BSON 值,Bson 枚舉類型定義了可表示為 BSON 值的所有類型。

示例:建立 Bson 執行個體

Bson 中的文檔由一組有序的鍵值對組成,類似于 JSON 中的 Object。另外,還包含 Bson 值所占的空間大小。

示例一:建立文檔執行個體

示例二:通路文檔成員

bson Crate 提供的序列化函數:

bson::to_bson: Encode a T Serializable into a BSON Value.

bson::to_document: Encode a T Serializable into a BSON Document.

bson Crate 提供的反序列化函數:

bson::from_bson: Decode a BSON Value into a T Deserializable.

bson::from_document: Decode a BSON Document into a T Deserializable.