1

so I stumble upon a problem where I need to instead of parsing pure value, run a function on that value and return the processed value from it into that field and I want to include that in the Serde JSON parser itself, I guess? confused emoji

I'm trying to parse Reddit Award and instead of icon_url, I want it to already in the parsing process to download the image (bytes of the image) and have that value instead of the URL. It would simplify so many things for me at the moment. Also would be cool if I could put some different field name for interaction with it. I was googling really a lot and I couldn't find absolutely anything about it, so I basically have no idea how to do that.

Would be very cool if I could do something like this

Original:

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Reward {
    pub description: String,
    pub icon_format: Option<String>,
    pub name: String,
    pub count: u32,
    pub icon_url: Url,
    pub coin_price: u64,
}

What I imagine:

#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Reward {
    pub description: String,
    pub icon_format: Option<String>,
    pub name: String,
    pub count: u32,
    #[download_image(field = "icon_url"]
    pub image: Bytes,
    pub coin_price: u64,
}

Any ideas? I think it's not even possible but I couldn't find anybody else that would like to do something like this on the internet so :/

If it's not possible I thought I could create two structs one with icon_url and second with image: Bytes and then just process it but that's just very ugly, clumsy etc...

  • Try looking at [`deserialize_with`](https://serde.rs/field-attrs.html#deserialize_with) – Jmb Aug 05 '20 at 06:27
  • @Jmb yup, deserialize_with seems to be the right solution. You might want to post that as an answer. – Ten Aug 06 '20 at 13:53

0 Answers0