akubera / bigdecimal-rs

Arbitrary precision decimal crate for Rust
Other
275 stars 71 forks source link

Add arbitrary precision support for serde_json deserialization #117

Open tenuous-guidance opened 10 months ago

tenuous-guidance commented 10 months ago

With the 0.4 changes, parsing decimals from JSON results in artefacts from those numbers going via f64. serde-json has support for abitrary precision parsing, which would solve this without reverting to 0.3 behaviour.

This is something I might be able to work on, but I wanted to check if it would be considered first. I think the way rust-decimal handled it is fairly good, and so would probably copy that behaviour if this was considered reasonable.

This is somewhat a duplicate of #113 but wanted to raise it separately as it's more focussed on this particular approach to fixing it.

akubera commented 10 months ago

Yeah, that sounds good to me. I'm not familiar with implementing serde transformations. Is it essentially a function mapping &str -> BigDecimal?

I've been splitting code into "impl" modules instead of putting everything in lib.rs. You can make impl_serde.rs and put code and tests in there. Branch off of trunk.

I think we had a limit of serde_json = "<1.0.101" due to incompatibilities with minimum-supported-rust-version 1.43. If that's an issue we'll merge this into an 0.5.0 branch. I'm trying not to stray too far from the num_ crates' MSRV of 1.31.

akubera commented 2 months ago

In v0.4.4 enable feature serde-json (with dash, not underscore) and use serde "with" the bigdecimal::serde::json_num serializer.

#[derive(Debug,Serialize,Deserialize)]
struct MyStruct {
    #[serde(with = "bigdecimal::serde::json_num")]
    number: BigDecimal,
}

Let me know if that works.