blockfrost-rust icon indicating copy to clipboard operation
blockfrost-rust copied to clipboard

Deserialize and serialize quantities as integers

Open marcospb19 opened this issue 4 years ago • 1 comments

Some JSON bodies pass integers as strings.

{
    "unit": "lovelace",
    "quantity": "500000"
}

Here's the current struct for this (simplified):

#[derive(Deserialize)]
pub struct Amount {
    pub unit: String,
    pub quantity: String,
}

It must be changed to:

#[derive(Deserialize)]
pub struct Amount {
    pub unit: String,
    #[serde_hacks]
    pub quantity: Integer,
}

Where Integer is our type alias for i128 and serde_hacks is the custom deserialization for that specific field.

Some options to customize this:

  1. https://docs.rs/serde_with/1.10.0/serde_with/#displayfromstr
  2. https://serde.rs/variant-attrs.html# (+ https://docs.rs/serde/1.0.130/serde/de/trait.Error.html)

marcospb19 avatar Sep 29 '21 17:09 marcospb19

Could be convenient to catch errors in the deserialization step and the users of the crate won't have to manually convert each field while trying to use them.

However, the conversion to integer needs to be treated.

marcospb19 avatar Sep 29 '21 17:09 marcospb19