-
-
Notifications
You must be signed in to change notification settings - Fork 492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unable to (de)serialize nested JSON Blobs using serde Traits #676
Comments
Hey @schulke-214, please refer to https://github.com/SeaQL/sea-orm/blob/master/issues/400/src/model.rs on how to implement necessary traits for custom model fields |
It comes from an external crate. We are unable to implement your traits for external types. Also i would expect sea-orm to be compliant with standard rust ecosystem traits such as Serialize and Deserialize instead of building your own type infrastructure around it. Most crates wont be compatible with sea orm this way. |
This isn't ideal but you can do it with newtype pattern. pub struct MySomeStruct(SomeStruct);
impl SomeTrait for MySomeStruct {
...
} |
If that is the only reaction to this issue we are not going to be able to use sea orm sadly. I think a lot of things are done excellent about it, but this is just some missing trait implementations on your side: https://github.com/SeaQL/sea-query/blob/master/src/value.rs#L304-L308 mod with_json {
...
impl<T> From<T> for sea_orm::Value
where
T: Serialize,
for<'de> T: Deserialize<'de>
{
fn from(json: T) -> Self {
if let Some(value) = serde_json::to_value(&json) {
return Self::Value::Json(Some(Box::new(value))
}
None
}
}
impl<T> Nullable for T
where
T: Serialize,
for<'de> T: Deserialize<'de>
{
fn null() -> Value {
Value::Json(None)
}
}
impl<T> ValueType for T
where
T: Serialize,
for<'de> T: Deserialize<'de>
{
fn try_from(v: Value) -> Result<Self, ValueTypeErr> {
match v {
Value::Json(Some(x)) => serde_json::from_value(x.as_ref()),
_ => Err(ValueTypeErr),
}
}
fn type_name() -> String {
"Json".to_owned()
}
fn column_type() -> ColumnType {
// take smh from the annotation #[sea_orm(db_type="X")
}
}
} |
@schulke-214 I think you raised an interesting use case |
Hey @schulke-214, feel free to review #696 |
Description
We have a database schema where we save (from the db pov) untyped json blobs into the database + some metadata columns. We have a serde compatible struct for describing the blobs on the rust side and expected to be able to use the
#[sea_orm(db_type = "Json")]
as annotation to be able to deserialize the JSON col into the rust struct on select.Steps to Reproduce
Rust
SQL
Expected Behavior
The details field of the Model should be deserialized from
json
/jsonb
columns into theSomeStruct
type in rust.Actual Behavior
Reproduces How Often
Every time.
Versions
0.7.1
Additional Information
None
The text was updated successfully, but these errors were encountered: