A library to improve Json responses in Actix-Web

Hey Rust community! For such a long time I always wanted a better way to type Actix-Web handlers that return a JSON object, but I never prioritized it to actually do something about it :sweat_smile:. So today I Just created a tiny library that exposes a helper type for this. It's use is extremely simple:

use actix_web::{get, web, Result};
use actix_json_response::JsonResponse;
use serde::Serialize;

#[derive(Serialize)]
struct MyObj {
    name: String,
}

#[get("/a/{name}")]
async fn index(name: web::Path<String>) -> Result<JsonResponse<MyObj>> {
    let my_obj = MyObj {
        name: name.to_string(),
    };
    Ok(my_obj.into())
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    use actix_web::{App, HttpServer};

    HttpServer::new(|| App::new().service(index))
        .bind(("127.0.0.1", 8080))?
        .run()
        .await
}

I hope other members of the community find it useful as well!

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.