0

I've got the following code:

use std::fmt::Debug;
use rocket::serde::{Serialize};
#[serde(crate = "rocket::serde")]

pub trait ComponentDto: Debug + Send + Sync + Serialize {}
use rocket::serde::{Deserialize, Serialize};


#[derive(Serialize)]
#[serde(crate = "rocket::serde")]

pub struct ParagraphDto {
    pub id: i32,
    pub uuid: String,
    pub user_id: i32,
    pub project_id: i32,
    pub position: i32,
    pub component: Box<dyn ComponentDto>,
    pub status: String,
    pub created_at: String,
    pub updated_at: String,
}

impl ParagraphDto {

How do I serialize trait object using rocket::serde?

Using rockett::serde as a normal struct to trait object results in the following error:

Compiling testa v0.1.0 (/mnt/repository/repository/projects/rust/testa)
error[E0038]: the trait `MyTrait` cannot be made into an object
    --> src/main.rs:21:20
     |
21 | fn getStruct() -> Box<dyn MyTrait> {
     | ^^^^^^^^^^^^^^^^^ `MyTrait` cannot be made into an object
     |
note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits. html#object-safety>
Giovanni Patruno
  • 651
  • 9
  • 15
WoFVi
  • 21
  • 1
  • Does this answer your question? [How to implement `serde::Serialize` for a boxed trait object?](https://stackoverflow.com/q/50021897/2189130) – kmdreko Apr 01 '22 at 19:06
  • https://stackoverflow.com/questions/50021897/how-to-implement-serdeserialize-for-a-boxed-trait-object I tried it, but it doesn't work, rocket framework needs rocket::serde::{Deserialize, Serialize} – WoFVi Apr 02 '22 at 16:45

0 Answers0