Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with serde enum untagged #4

Closed
pjankiewicz opened this issue Mar 31, 2021 · 4 comments
Closed

Problem with serde enum untagged #4

pjankiewicz opened this issue Mar 31, 2021 · 4 comments

Comments

@pjankiewicz
Copy link

pjankiewicz commented Mar 31, 2021

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

@pjankiewicz
Copy link
Author

pjankiewicz commented Mar 31, 2021

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.

@thekuom
Copy link

thekuom commented May 2, 2021

@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)

@seanpianka
Copy link

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" }

@LukeMathWalker
Copy link
Owner

LukeMathWalker commented Aug 20, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants