Some brief context: we're storing uuids in our database; these function as ids. We'd like to present these to various http clients as base62 encoded representations. Ideally we wouldn't have to remember to do this every time we encode a struct. It's worth noting that we're also leveraging sqlx and lean on structs via query_as!
. For example:
#[derive(Debug, Deserialize, FromRow, Serialize)]
pub struct Widget {
pub id: Uuid,
pub name: String,
}
And then later:
let widget = query_as!(Widget, r#"select * from widgets where id = $1"#, id).fetch_one(db_conn).await?;
Ideally we'd like to leverage this same struct to send data to the client and likewise when pulling data off the wire. We'd also like to convert the id when doing so (from a base62 encoding to uuid when receiving and from uuid to base62 encoding when sending).
How can we control the behavior of JSON serialization and deserialization specifically while continuing to leverage these structs?