1 个不稳定版本

0.12.0 2020年11月10日

#2248编码

Download history 115/week @ 2024-04-06 108/week @ 2024-04-13 206/week @ 2024-04-20 201/week @ 2024-04-27 127/week @ 2024-05-04 352/week @ 2024-05-11 153/week @ 2024-05-18 147/week @ 2024-05-25 149/week @ 2024-06-01 146/week @ 2024-06-08 230/week @ 2024-06-15 149/week @ 2024-06-22 180/week @ 2024-06-29 232/week @ 2024-07-06 134/week @ 2024-07-13 108/week @ 2024-07-20

669 每月下载次数
18 个crate中使用了(2 个直接使用)

MIT/Apache

140KB
3.5K SLoC

Serde CBOR

Build Status Crates.io Documentation

本crate实现了RFC 7049中的 Concise Binary Object Representation。它基于Serde,Rust的通用序列化框架。CBOR为JSON数据模型的超集提供了一个小型且解析速度非常快的二进制编码。

用法

Serde CBOR支持Rust 1.40及以上版本。将以下内容添加到您的Cargo.toml

[dependencies]
serde_cbor = "0.11.1"

存储和加载Rust类型非常容易,只需对程序代码进行最小修改。

use serde_derive::{Deserialize, Serialize};
use std::error::Error;
use std::fs::File;

// Types annotated with `Serialize` can be stored as CBOR.
// To be able to load them again add `Deserialize`.
#[derive(Debug, Serialize, Deserialize)]
struct Mascot {
    name: String,
    species: String,
    year_of_birth: u32,
}

fn main() -> Result<(), Box<dyn Error>> {
    let ferris = Mascot {
        name: "Ferris".to_owned(),
        species: "crab".to_owned(),
        year_of_birth: 2015,
    };

    let ferris_file = File::create("examples/ferris.cbor")?;
    // Write Ferris to the given file.
    // Instead of a file you can use any type that implements `io::Write`
    // like a HTTP body, database connection etc.
    serde_cbor::to_writer(ferris_file, &ferris)?;

    let tux_file = File::open("examples/tux.cbor")?;
    // Load Tux from a file.
    // Serde CBOR performs roundtrip serialization meaning that
    // the data will not change in any way.
    let tux: Mascot = serde_cbor::from_reader(tux_file)?;

    println!("{:?}", tux);
    // prints: Mascot { name: "Tux", species: "penguin", year_of_birth: 1996 }

    Ok(())
}

有很多选项可供自定义格式。要操作无类型的CBOR值,请查看Value类型。

许可协议

许可协议为以下之一

任选其一。

贡献

除非您明确声明,否则任何有意提交以包含在作品中的贡献,根据Apache-2.0许可协议定义,将按上述方式双许可,无需任何附加条款或条件。

依赖关系

~350–590KB
~13K SLoC