-
Notifications
You must be signed in to change notification settings - Fork 72
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
bug: serde_json/arbitrary_precision + PickFirst causes issues #549
Comments
This is not a problem of this crate, but rather how
/*
[dependencies]
serde.version = "*"
serde.features = ["derive"]
serde_json.version = "*"
# Comment the next line and see how the code successfully deserializes both values
serde_json.features = ["arbitrary_precision"]
*/
#[derive(Debug, serde::Serialize, serde::Deserialize)]
#[serde(untagged)]
enum Foo {
S(String),
F(f64),
}
fn main() {
let f: Foo = serde_json::from_str(r#"123"#).unwrap();
dbg!(f);
let f: Foo = serde_json::from_str(r#"8.0e-5"#).unwrap();
dbg!(f);
} But I do have a workaround for you. You can add a third variant to the /*
[dependencies]
serde.version = "*"
serde.features = ["derive"]
serde_json.version = "*"
serde_json.features = ["arbitrary_precision"]
serde_with = "*"
*/
use serde::Deserialize;
use serde_with::{serde_as, DisplayFromStr, PickFirst};
serde_with::serde_conv! {
IntermediaryJsonValue,
f64,
|f: &f64| *f,
|v: serde_json::Value| f64::deserialize(v)
}
fn main() {
#[serde_as]
#[derive(Debug, Clone, PartialEq, Deserialize)]
struct MaybeFloatMaybeString(
#[serde_as(as = "PickFirst<(DisplayFromStr, _, IntermediaryJsonValue)>")] f64,
);
println!("{}", serde_json::from_str::<f64>(r#"8.0e-5"#).unwrap());
println!(
"{}",
serde_json::from_str::<MaybeFloatMaybeString>(r#"8.0e-5"#)
.unwrap()
.0
);
println!(
"{}",
serde_json::from_str::<MaybeFloatMaybeString>(r#""8.0e-5""#)
.unwrap()
.0
);
} |
Thanks a bunch for your response & explanation, really above and beyond :) |
I have a peculiar bug where when I enable serde_json's
arbitrary_precision
feature flag and deserialize andf64
usingPickFirst
it inexplicably fails. I have created a minimal reproduction here:https://github.com/OliverNChalk/serde-with-repro/blob/master/src/main.rs
There are two branches:
master
- code is brokenfixed
- code works (i just turn the feature off)Thought I would file here as I assume you guys would be most familiar with the internals of what this derive macro is doing and how this behavior could be surfacing.
The text was updated successfully, but these errors were encountered: