Expand description
JSON support for the Churust web framework.
This crate provides two things:
-
Json<T>— a dual-purpose wrapper that deserializes a JSON request body when used as a handler argument (viaFromCall) and serializesTto aapplication/jsonresponse when used as a handler return value (viaIntoResponse). -
ContentNegotiation— an optionalPluginthat intercepts plain-text error responses (status ≥ 400) and re-encodes them as structured JSON so that API clients always receive a consistent{"error":"…","status":N}body.
§Quick start
use churust_core::{Churust, Call, TestClient};
use churust_json::{Json, ContentNegotiation};
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Greeting { name: String }
let app = Churust::server()
.install(ContentNegotiation::new())
.routing(|r| {
r.post("/greet", |Json(body): Json<Greeting>| async move {
Json(Greeting { name: format!("Hello, {}!", body.name) })
});
})
.build();
let res = TestClient::new(app)
.post("/greet")
.header("content-type", "application/json")
.body(r#"{"name":"Alice"}"#)
.send()
.await;
assert_eq!(res.status().as_u16(), 200);
assert_eq!(res.header("content-type"), Some("application/json"));Structs§
- Content
Negotiation - Plugin that converts plain-text error responses into structured JSON.
- Json
- A JSON extractor and responder.
Traits§
- Call
Json - Call-style JSON helpers — v1 design §5.1.