Problem with serde enum untagged
pjankiewicz opened this issue ยท 4 comments
I had the weirdest bug ever. I took me 3 hours to narrow down the source below is a minimal example of the error
./src/main.rs
#[macro_use]
extern crate serde_derive;
use serde;
use serde_json;
use serde::{Serialize, Deserialize};
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Data {
Integer(u64),
Pair(String, String),
}
fn main() {
let w = Data::Integer(1);
let w_str = serde_json::to_string(&w).unwrap();
println!("{:?}", w_str);
let w_deser = serde_json::from_str::<Data>(&w_str).unwrap();
println!("{:?}", w_deser);
}
Cargo.toml
[package]
name = "test"
version = "0.1.0"
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[[bin]]
path = "src/main.rs"
name = "main"
[dependencies]
serde = "1.0.124"
serde_derive = "1.0.124"
serde_json = "1.0.64"
When adding tracing-bunyan-formatter = "0.1.7"
to dependenies main throws an error. Tested also with "0.2.0".
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Error("data did not match any variant of untagged enum Data", line: 0, column: 0)', src/main.rs:19:56
EDIT: Just noticed this issue #3
Adding to dependencies fixed the issue { version = "0.2", default-features = false }.
Not sure if this shouldn't be the default because it is very hard to find the issue and I'm sure I won't be the last person to report this.
#[derive(Debug, Serialize, Deserialize)]
#[serde(untagged)]
enum Data {
Integer(u64),
Pair(String, String),
}
is an example from serde documentation and it should serialize and deserialize properly when importing any library - https://serde.rs/enum-representations.html.
@pjankiewicz, looks like this is a result of tracing-bunyan-formatter including the arbitrary-precision feature by default, which causes a known issue with serde tag: serde-rs/json#505
Alternate workaround is mentioned here: serde-rs/json#505 (comment)
Thanks for opening this issue! It validates the many hours I spent banging my head against my desk attempting to debug some unit test failures ๐.
The following resolves this issue:
[dependencies]
tracing-bunyan-formatter = { default-features = false, version = "0.2" }
The arbitrary-precision
feature is now disabled by default in tracing-bunyan-formatter
0.2.5
.
I've added a warning linking to this issue in the README where we advertise the feature to avoid nasty surprises for those choosing to use it.