【问题标题】:Serializing a Vec<u8> to CBOR byte string in Rust using serde_cbor使用 serde_cbor 在 Rust 中将 Vec<u8> 序列化为 CBOR 字节字符串
【发布时间】:2020-12-28 19:30:31
【问题描述】:

我想使用serde_cborVec&lt;u8&gt; 编码为CBOR 字节字符串。我尝试了以下代码:

use serde_cbor::{to_vec}

let data = vec![0x01, 0x23, 0x45, 0x67, 0x89, 0xab];
let encoded_data = to_vec(&data)?;
println!("encoded_data: {:x?}", encoded_data);

生成以下输出:

encoded_data: [86, 1, 18, 23, 18, 45, 18, 67, 18, 89, 18, ab]

这意味着所有元素都被编码为单个整数。但是,我想将向量编码为 CBOR 字节字符串,即:

46              # bytes(6)
   0123456789AB # "\x01#Eg\x89\xAB"

我该怎么做?

【问题讨论】:

    标签: rust serde cbor


    【解决方案1】:

    使用serde_bytes crateserde_bytes::Bytes 并改为使用to_vec(&amp;Bytes::new(&amp;data[..]))?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-10-28
      • 2020-01-22
      • 2023-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      • 1970-01-01
      相关资源
      最近更新 更多