Crate serde_tran

Crate serde_tran 

Source
Expand description

§serde_tran: helper to encode/decode your JSON data

§Background

When we use the HTTP protocol (or other transmission protocols), it is convenient and common to use the Json data format to store it in the HTTP request body or response body.

However, when we transmit a large amount of data (for example, a large array, and the key in the Map type data is relatively long), it is not efficient to use Json data directly.

Therefore, you can consider using this library. This library will help you encapsulate Json type data (that is, impl Serialize) with an extra layer (the result is still Json, but the data is more compact).

§Usage

First, we define a struct in our example, which impl Serialize:

use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
pub struct MyStruct {
    // fields...
}

pub fn example() {
    let s = MyStruct {};

    // 1. convert it to serde_tran::Json
    let json = serde_tran::to_json(&s).unwrap();

    // json is something like: {"f": "base64", "v": "(base64 encoded string)"}
    // you can use json.to_string() to get the json string.

    // 2. convert it back to MyStruct
    let ds: MyStruct = json.to_value().unwrap();

    assert_eq!(s, ds); // they are the same (remember to derive PartialEq to use macro assert_eq!)
}

More examples, see the folder examples.

Structs§

Json
Json stores the data and encoding.

Enums§

Base
ErrorKind

Functions§

from_base64
convert data from base64 string to T
from_json_slice
convert bytes back to Json, then you can use Json::to_value to get your custom data.
from_slice
convert bytes into T
to_base64
convert data to base64 string
to_json
convert given data into Json, then you can use Json::to_string or Json::to_vec to get a json string.
to_json_base64
convert given data into Json, where format is Base::Base64
to_vec
convert data to bytes

Type Aliases§

Format
Result