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

JSON deserialization from payload fails with i128 is not supported #709

Closed
pocesar opened this issue Oct 2, 2023 · 0 comments · Fixed by #712
Closed

JSON deserialization from payload fails with i128 is not supported #709

pocesar opened this issue Oct 2, 2023 · 0 comments · Fixed by #712

Comments

@pocesar
Copy link

pocesar commented Oct 2, 2023

MetadatumRendition enum got a i128, but fails because of a serde bug when using #[serde(flatten)]. enabling arbitrary_precision flag doesn't help

see serde-rs/serde#1183
serde-rs/json#625

simple repro:

    let data = json!(oura::model::MetadataRecord {
      label: "hello".to_string(),
      content: oura::model::MetadatumRendition::IntScalar(2_i128.pow(90)),
    });

    let json_value: oura::model::MetadataRecord = serde_json::from_value(data).unwrap(); // unwrap fails here, Error("i128 is not supported", line: 0, column: 0)
    let parsed = serde_json::to_string(&json_value).unwrap();

    let data = serde_json::from_str::<oura::model::MetadataRecord>(&parsed).unwrap();
    println!("{:?}", parsed);
    println!("{:?}", data);

this change works:

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct MetadataRecord {
    pub label: String,
    // no #[serde(flatten)]
    pub content: MetadatumRendition,
}

a couple of solutions:

  • remove the flatten. makes the nested struct messy
  • serialize/deserialize to BigInt instead, including anything that is higher than Number.MAX_SAFE_INTEGER from JS
  • create a custom se/deserializer to output as those >= Number.MAX_SAFE_INTEGER as string

regarding the interop in JSON types from the RFC https://datatracker.ietf.org/doc/html/rfc7159#page-7:

Note that when such software is used, numbers that are integers and
are in the range [-(2^53)+1, (2^53)-1] are interoperable in the
sense that implementations will agree exactly on their numeric
values

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

Successfully merging a pull request may close this issue.

2 participants