Function warp::reply::json

source ·
pub fn json<T>(val: &T) -> Json
where T: Serialize,
Expand description

Convert the value into a Reply with the value encoded as JSON.

The passed value must implement Serialize. Many collections do, and custom domain types can have Serialize derived.

§Example

use warp::Filter;

// GET /ids returns a `200 OK` with a JSON array of ids:
// `[1, 3, 7, 13]`
let route = warp::path("ids")
    .map(|| {
        let our_ids = vec![1, 3, 7, 13];
        warp::reply::json(&our_ids)
    });

§Note

If a type fails to be serialized into JSON, the error is logged at the error level, and the returned impl Reply will be an empty 500 Internal Server Error response.