2016-06-23 42 views
3

我试图用SERDE宏序列化JSON但我只得到这个错误:Serde JSON序列化错误?

src/models/pulse.rs:89:28: 89:49 error: the trait bound `models::pulse::Pulse: serde::ser::Serialize` is not satisfied [E0277] 
src/models/pulse.rs:89  if let Ok(bulk_string) = serde_json::to_string(&self) { 
                ^~~~~~~~~~~~~~~~~~~~~ 
src/models/pulse.rs:89:28: 89:49 help: run `rustc --explain E0277` to see a detailed explanation 
src/models/pulse.rs:89:28: 89:49 note: required because of the requirements on the impl of `serde::ser::Serialize` for `&mut models::pulse::Pulse` 
src/models/pulse.rs:89:28: 89:49 note: required by `serde_json::to_string` 

我似乎没有有什么错误的代码。

#[derive(Serialize, Deserialize)] 
pub struct Pulse { 
    #[serde(skip_serializing_if="Option::is_none")] 
    id: Option<u64>, 

    #[serde(skip_serializing_if="Option::is_none")] 
    category: Option<i64>, 

    #[serde(skip_serializing_if="Option::is_none")] 
    title: Option<String>, 

} 

它是依赖性错误吗?或者是我的代码有问题?我使用

[dependencies.serde] 
git = "https://github.com/serde-rs/serde.git" 
[dependencies.serde_macros] 
git = "https://github.com/serde-rs/serde.git" 
[dependencies.serde_json] 
git = "https://github.com/serde-rs/json.git" 

我有个问题,为什么我使用git依赖关系。原因是我得到:

/.cargo/registry/src/github.com-1ecc6299db9ec823/aster- 

0.18.0/src/mac.rs:6:5: 6:39 error: unresolved import `syntax::feature_gate::GatedCfgAttr`. There is no `GatedCfgAttr` in `syntax::feature_gate`. Did you mean to use `GatedCfg`? [E0432] 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6 use syntax::feature_gate::GatedCfgAttr; 
                           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:6:5: 6:39 help: run `rustc --explain E0432` to see a detailed explanation 
    Compiling num-traits v0.1.32 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 error: this function takes 4 parameters but 5 parameters were supplied [E0061] 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113  let mut cx = ExtCtxt::new(sess, cfg, ecfg, feature_gated_cfgs, macro_loader); 
                               ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 help: run `rustc --explain E0061` to see a detailed explanation 
/.cargo/registry/src/github.com-1ecc6299db9ec823/aster-0.18.0/src/mac.rs:113:18: 113:81 note: the following parameter types were expected: &syntax::parse::ParseSess, std::vec::Vec<syntax::ptr::P<syntax::codemap::Spanned<syntax::ast::MetaItemKind>>>, syntax::ext::expand::ExpansionConfig<'_>, &mut syntax::ext::base::MacroLoader 

我认为这个问题可能是你需要每晚使用。

+2

你为什么使用'git'依赖?你应该通过crates.io使用它们。 –

+0

因为如果我使用箱子:我得到另一个错误。 @ker我现在已经更新了这个问题。 – user3384741

+2

运行'货物更新'(您看到的问题昨天已经修复,只发生在夜间)或使用稳定的编译器 –

回答

1

rustc 1.20.0-nightly,serde 1.0.11serde-json 1.0.2开始,此代码编译时没有问题。

在附注中,你应该使用crates.io依赖而不是git。