-
#[derive(AsExpression, FromSqlRow, Debug)]
#[diesel(sql_type = Text)]
pub struct JsonWrapper<T: serde::de::DeserializeOwned + serde::Serialize + fmt::Debug>(pub T);
impl<T: serde::de::DeserializeOwned + serde::Serialize + fmt::Debug> FromSql<Text, Sqlite>
for JsonWrapper<T>
{
fn from_sql(bytes: diesel::backend::RawValue<'_, Sqlite>) -> diesel::deserialize::Result<Self> {
let bytes = <Vec<u8>>::from_sql(bytes)?;
let inner = serde_json::from_slice::<T>(&bytes)?;
Ok(JsonWrapper(inner))
}
}
impl<T: serde::de::DeserializeOwned + serde::Serialize + fmt::Debug> ToSql<Text, Sqlite>
for JsonWrapper<T>
{
fn to_sql<'b>(
&'b self,
out: &mut diesel::serialize::Output<'b, '_, Sqlite>,
) -> diesel::serialize::Result {
todo!()
}
} I have tried this: <str as ToSql<Text, Sqlite>>::to_sql(&serde_json::to_string(&self.0).unwrap(), out) but the complier complains that: From what I understand, serde_json does not support returning a reference, so could someone provide assistance with resolving this issue? |
Beta Was this translation helpful? Give feedback.
Answered by
weiznich
Mar 29, 2023
Replies: 1 comment 2 replies
-
See the relevant section of the migration guide or the documentation of |
Beta Was this translation helpful? Give feedback.
2 replies
Answer selected by
lisiur
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See the relevant section of the migration guide or the documentation of
ToSql
for information on how to resolve these kind of issues.