Skip to main content

Crate churust_json

Crate churust_json 

Source
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 (via FromCall) and serializes T to a application/json response when used as a handler return value (via IntoResponse).

  • ContentNegotiation — an optional Plugin that 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§

ContentNegotiation
Plugin that converts plain-text error responses into structured JSON.
Json
A JSON extractor and responder.

Traits§

CallJson
Call-style JSON helpers — v1 design §5.1.