-
I have a struct called User and the field ID has a type of Thing but when it gets serialized with Serde it returns a map but I want it to produce just a string of the record like this |
Beta Was this translation helpful? Give feedback.
Answered by
skriems
Sep 14, 2023
Replies: 1 comment 2 replies
-
I needed to do that for an async-graphql server, knowing that this obviously has downsides, in the sense that I'm limiting myself to use string id's only... anyway, for now I just needed that. use serde::{Deserialize, Deserializer};
use surrealdb::sql::Thing;
fn thing_to_string<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
D: Deserializer<'de>,
{
let t = Thing::deserialize(deserializer)?;
Ok(Some(t.to_raw()))
}
#[derive(Deserialize, Serialize)]
pub struct Foo {
#[serde(deserialize_with = "thing_to_string")]
id: Option<String>,
...
} Hope that helps |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
mimib00
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I needed to do that for an async-graphql server, knowing that this obviously has downsides, in the sense that I'm limiting myself to use string id's only... anyway, for now I just needed that.
Hope that helps