How do I convert diesel results to JSON

If I have this code:

let results = posts.filter(published.eq(true))
    .limit(5)
    .load::<Post>(&connection)
    .expect("Error loading posts");

as in the Diesel guide.

How do I convert the "results" to JSON for Actix Web output, like in the code below?

HttpResponse::Ok().json(results)

Thanks!

Add the serde crate and add a #[derive(Serialize)] to the Post struct. (see here for details).

If you wish to receive it as JSON too, you can derive Deserialize.

2 Likes

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.