#rocket #json #web #server #api

rocketjson_data

Crate for working with Json and Rocket

8 stable releases

1.1.2 Oct 8, 2021
1.1.1 Sep 22, 2021
1.1.0 Sep 19, 2021
1.0.5 Sep 1, 2021
1.0.3 Aug 31, 2021

#448 in Web programming

Download history 62/week @ 2021-08-26 33/week @ 2021-09-02 6/week @ 2021-09-09 32/week @ 2021-09-16 9/week @ 2021-09-23 17/week @ 2021-09-30 29/week @ 2021-10-07

55 downloads per month
Used in 2 crates

MIT/Apache

15KB
215 lines

rocketjson

Current Crates.io Version

Crate for working with Json and Rocket.
Ultimately the goal is to have validated Structs enter and leave the endpoint as Json while having everything happen in the background.

Documentation

Documentation is on docs.rs

Example

#[macro_use] extern crate rocket;

#[derive(serde::Deserialize, validator::Validate, rocketjson::JsonBody)]
pub struct RegisterRequest {
   #[validate(length(min = 1))]
   username: String 
}

#[derive(serde::Serialize)]
pub struct RegisterResponse {
   message: String
}

#[post("/register", data="<data>")]
pub fn register(data: RegisterRequest) -> rocketjson::ApiResponse<RegisterResponse> {
   rocketjson::ApiResponse::new(rocket::http::Status::Ok, RegisterResponse { message: format!("Welcome {}", data.username) })
}

#[launch]
fn rocket() -> _ {
    rocket::build()
        .mount("/", routes![register]).
        register("/", vec![rocketjson::error::get_catcher()])
}
  • Input
{
    "username": "testuser"
}
  • Output 200 OK
{
    "message": "Welcome testuser"
}
  • Input
{
    "username": ""
}
  • Output 400 Bad Request
{
    "username": [
        {
            "code": "length",
            "message": null,
            "params": {
                "value": "",
                "min": 1
            }
        }
    ]
}

License

The license can be chosen to be either of the following:

Dependencies

~30MB
~677K SLoC