Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/rust/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Rust 如何部分反序列化JSON对象?_Rust_Serde_Serde Json - Fatal编程技术网

Rust 如何部分反序列化JSON对象?

Rust 如何部分反序列化JSON对象?,rust,serde,serde-json,Rust,Serde,Serde Json,我有一个JSON对象: {"content":{"foo":1,"bar":2},"signature":"3f5ab1..."} 使用以下方法将其反序列化为自定义类型已经可以正常工作: let s: SignedContent = serde_json::from_str(string)?; 我想要的是{“foo”:1,“bar”:2}作为&[u8]切片,这样我就可以检查签名了 (我知道关于

我有一个JSON对象:

{"content":{"foo":1,"bar":2},"signature":"3f5ab1..."}
使用以下方法将其反序列化为自定义类型已经可以正常工作:

let s: SignedContent = serde_json::from_str(string)?;
我想要的是
{“foo”:1,“bar”:2}
作为
&[u8]
切片,这样我就可以检查签名了

(我知道关于规范化JSON表示的问题,并有相应的缓解措施。)

目前,我正在浪费时间将
内容
对象(在
SignedContent
对象中)重新序列化为字符串,并从中获取八位字节

有没有更有效的方法?

看起来像是的作业(可使用“原始值”功能)

引用包含输入数据中单个有效JSON值的字节范围

RawValue
可用于将有效负载的部分解析推迟到以后,或者在有效负载的部分只需要逐字传输到不同的输出对象的情况下,完全避免解析有效负载

序列化时,此类型的值将保留其原始格式,并且不会缩小或精确打印

用法如下:

#[derive(Deserialize)]
struct SignedContent<'a> {

    #[serde(borrow)]
    content: &'a RawValue,

    // or without the 'a
    //content: Box<RawValue>
}
#[派生(反序列化)]
结构签名内容