序列化结构上的子属性似乎不起作用
Serializing sub properties on a struct doesn't seem to work
我正在尝试序列化以下 Result
对象,但出现错误,因为虽然它适用于某些属性,但似乎不适用于 path
,即使所有涉及的元素都有 implementations provided by Serde.
#[macro_use]
extern crate serde;
extern crate rocket;
use rocket_contrib::json::Json;
use std::rc::Rc;
#[derive(Serialize)]
struct Result {
success: bool,
path: Vec<Rc<GraphNode>>,
visited_count: u32,
}
struct GraphNode {
value: u32,
parent: Option<Rc<GraphNode>>,
}
fn main(){}
fn index() -> Json<Result> {
Json(Result {
success: true,
path: vec![],
visited_count: 1,
})
}
Playground,虽然我没法让它拉进火箭箱,但它一定不是最受欢迎的100个之一。
error[E0277]: the trait bound `std::rc::Rc<GraphNode>: serde::Serialize` is not satisfied
--> src/main.rs:11:5
|
11 | path: Vec<Rc<GraphNode>>,
| ^^^^ the trait `serde::Serialize` is not implemented for `std::rc::Rc<GraphNode>`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::vec::Vec<std::rc::Rc<GraphNode>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
根据我的理解,#[derive(Serialize)]
应该会自动创建一个 serde 可以使用的序列化方法。但是我希望它也适用于这些属性。我是否需要为所有类型创建结构,然后为所有这些结构派生 Serialize
?
我需要做些什么来启用它吗?
以下箱子正在使用中:
rocket = "*"
serde = { version = "1.0", features = ["derive"] }
rocket_contrib = "*"
the trait bound `std::rc::Rc<GraphNode>: serde::Serialize` is not satisfied
这意味着 Rc
而不是 实现 Serialize
。参见 。长话短说:
serde = { version = "1.0", features = ["derive", "rc"] }
添加后,错误消息变为:
error[E0277]: the trait bound `GraphNode: serde::Serialize` is not satisfied
--> src/main.rs:11:5
|
11 | path: Vec<Rc<GraphNode>>,
| ^^^^ the trait `serde::Serialize` is not implemented for `GraphNode`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::rc::Rc<GraphNode>`
= note: required because of the requirements on the impl of `serde::Serialize` for `std::vec::Vec<std::rc::Rc<GraphNode>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
那是因为每个需要序列化的类型必须实现Serialize
:
#[derive(Serialize)]
struct GraphNode {
我正在尝试序列化以下 Result
对象,但出现错误,因为虽然它适用于某些属性,但似乎不适用于 path
,即使所有涉及的元素都有 implementations provided by Serde.
#[macro_use]
extern crate serde;
extern crate rocket;
use rocket_contrib::json::Json;
use std::rc::Rc;
#[derive(Serialize)]
struct Result {
success: bool,
path: Vec<Rc<GraphNode>>,
visited_count: u32,
}
struct GraphNode {
value: u32,
parent: Option<Rc<GraphNode>>,
}
fn main(){}
fn index() -> Json<Result> {
Json(Result {
success: true,
path: vec![],
visited_count: 1,
})
}
Playground,虽然我没法让它拉进火箭箱,但它一定不是最受欢迎的100个之一。
error[E0277]: the trait bound `std::rc::Rc<GraphNode>: serde::Serialize` is not satisfied
--> src/main.rs:11:5
|
11 | path: Vec<Rc<GraphNode>>,
| ^^^^ the trait `serde::Serialize` is not implemented for `std::rc::Rc<GraphNode>`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::vec::Vec<std::rc::Rc<GraphNode>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
根据我的理解,#[derive(Serialize)]
应该会自动创建一个 serde 可以使用的序列化方法。但是我希望它也适用于这些属性。我是否需要为所有类型创建结构,然后为所有这些结构派生 Serialize
?
我需要做些什么来启用它吗?
以下箱子正在使用中:
rocket = "*"
serde = { version = "1.0", features = ["derive"] }
rocket_contrib = "*"
the trait bound `std::rc::Rc<GraphNode>: serde::Serialize` is not satisfied
这意味着 Rc
而不是 实现 Serialize
。参见
serde = { version = "1.0", features = ["derive", "rc"] }
添加后,错误消息变为:
error[E0277]: the trait bound `GraphNode: serde::Serialize` is not satisfied
--> src/main.rs:11:5
|
11 | path: Vec<Rc<GraphNode>>,
| ^^^^ the trait `serde::Serialize` is not implemented for `GraphNode`
|
= note: required because of the requirements on the impl of `serde::Serialize` for `std::rc::Rc<GraphNode>`
= note: required because of the requirements on the impl of `serde::Serialize` for `std::vec::Vec<std::rc::Rc<GraphNode>>`
= note: required by `serde::ser::SerializeStruct::serialize_field`
那是因为每个需要序列化的类型必须实现Serialize
:
#[derive(Serialize)]
struct GraphNode {