Serde: #[serde(default="false")] failed to parse path: "false"

Created on 2 Sep 2017  路  3Comments  路  Source: serde-rs/serde

Code:

#[macro_use]
extern crate serde_derive;
extern crate serde;

#[derive(Deserialize)]
struct A {
  #[serde(default="false")]
  a: bool,
}

fn main() {
    println!("Hello, world!");
}

Link to playground:
https://play.rust-lang.org/?gist=8792a9fd44c0fff060f54e8f3b5281c3&version=stable

duplicate

Most helpful comment

For anyone coming across this and #368 has not been merged, a workaround can be:

#[macro_use]
extern crate serde_derive;
extern crate serde;

fn default_as_false() -> bool {
    false
}

#[derive(Deserialize)]
struct A {
  #[serde(default="default_as_false")]
  a: bool,
}

fn main() {
    println!("Hello, world!");
}

Link to playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=12e58011d3cb46ba08e849e8a20592d5

All 3 comments

It seems you can work around it by creating a constant that is set to false.

Thanks! We are tracking this in #368.

For anyone coming across this and #368 has not been merged, a workaround can be:

#[macro_use]
extern crate serde_derive;
extern crate serde;

fn default_as_false() -> bool {
    false
}

#[derive(Deserialize)]
struct A {
  #[serde(default="default_as_false")]
  a: bool,
}

fn main() {
    println!("Hello, world!");
}

Link to playground:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2015&gist=12e58011d3cb46ba08e849e8a20592d5

Was this page helpful?
0 / 5 - 0 ratings