Comments (2)
This is not a problem of this crate, but rather how serde_json
chooses to transmit large numbers with the arbitrary_precision
feature set and serde
s failure to support extended data models and that any form of buffering (such as untagged enums) break various features (serde-rs/serde#1183).
There are also issues for this in the serde_json
repo: serde-rs/json#740 and serde-rs/json#559.
PickFirst
uses an untagged
enum internally. But due to serde-rs/serde#1183 this can break stuff. Here you see the same problem without PickFirst
.
https://www.rustexplorer.com/b/p77mis
/*
[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 PickFirst
, which first buffers into a serde_json::Value
. That type understands how arbitrary precision values are encoded and can then be used to deserialize a f64 from it. The f64 then no longer has the arbitrary precision.
https://www.rustexplorer.com/b/77w4yn
/*
[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
);
}
from serde_with.
Thanks a bunch for your response & explanation, really above and beyond :)
from serde_with.
Related Issues (20)
- Vec <-> BTreeMap conversion not available in `no_std` mode
- Non string values in attribute macro HOT 3
- Using serde_with::rust::sets_last_value_wins with serde_as notation HOT 5
- Add `skip_serializing_empty` macro HOT 2
- use serde_with::apply within macro HOT 3
- Deserialize a map into a Vec of keys HOT 2
- deserialize json for String and i32 HOT 1
- Double option pattern with subserializer HOT 2
- Enable std/alloc without other features like chrono HOT 4
- How to combine `double_option` and `serde_as`? HOT 2
- Confusing/Incorrect PickFirst behavior HOT 3
- Compilation fails for targets without `alloc::sync`/atomic pointers
- Wishlist: Blocked on upstream changes
- Suggestion(Seq): Map to Vector of structs HOT 5
- 2.3.0 breaks `HashMap` serialization as `Vec` of pairs with `bincode` HOT 2
- Better Document behavior of DurationSeconds HOT 3
- Deserialization doesn't work on hashmap if both generic parameters are constrainted with DeserializeAs types HOT 3
- `DefaultOnError` not behaving like the examples HOT 1
- applying serde_as to whole struct. HOT 6
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serde_with.