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

Deserialize and serialize quantities as integers #6

Open
marcospb19 opened this issue Sep 29, 2021 · 1 comment
Open

Deserialize and serialize quantities as integers #6

marcospb19 opened this issue Sep 29, 2021 · 1 comment
Labels
enhancement New feature or request

Comments

@marcospb19
Copy link
Collaborator

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 marcospb19 added the enhancement New feature or request label Sep 29, 2021
@marcospb19
Copy link
Collaborator Author

marcospb19 commented Sep 29, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant