GithubHelp home page GithubHelp logo

Comments (2)

jonasbb avatar jonasbb commented on June 9, 2024

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 serdes 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.

OliverNChalk avatar OliverNChalk commented on June 9, 2024

Thanks a bunch for your response & explanation, really above and beyond :)

from serde_with.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.