0

I'm trying to implement a serializer for my struct:

#[derive(Queryable, Debug, Clone, Serialize, Deserialize)]
struct One {
    two_id: i32,
}

#[derive(Queryable, Debug, Clone, Serialize, Deserialize)]
struct Two {
    id: i32,
}

I'd like to get struct One from the database with diesel and print it with serde. I found the serialize_with attribute which can be applied to two_id. How would I implement such a method with the given interface: fn<'de, D>(D) -> Result<T, D::Error> where D: Deserializer<'de> to invoke json!(get_two_from_db()) on my queried object?

I'd like to be able to call json!(one) and automatically get the joined objects.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Koschi13
  • 549
  • 1
  • 4
  • 21
  • It looks like your question might be answered by the answers of [How to transform fields during serialization using Serde?](https://stackoverflow.com/q/39383809/155423). If not, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Nov 20 '19 at 15:55
  • 1
    Alright @Shepmaster the other question gives me the basics to implement is by myself. Thank you, this Question can be marked as already answered. – Koschi13 Nov 20 '19 at 16:05
  • That's great your question was answered! Depending on your surrounding program, you may want to think if there are more SQL-efficient ways to get all the data you want in fewer queries (perhaps just one...). It's not always possible, though. – Shepmaster Nov 20 '19 at 16:09

0 Answers0