#api-response #api #json #serde #serde-json

jsend

A lightweight Rust crate for structuring API responses in the JSend format

3 stable releases

1.0.2 Mar 16, 2024

#1630 in Web programming

MIT license

11KB
108 lines

jsend

Crates.io Version docs.rs

An implementation of the JSend specification in Rust.

See the crate documention, the examples, and the JSend specification for more information.


lib.rs:

The jsend crate provides an implementation of the JSend specification for API responses in Rust applications.

Usage

Add jsend to your Cargo.toml:

[dependencies]
jsend = "1.0"
serde = { version = "1.0", features = ["derive"] }

Basic Example

use jsend::JSendResponse;
use std::collections::HashMap;

// Success response with data
let data = Some(HashMap::from([("key", "value")]));
let response = JSendResponse::success(data);
println!("{}", serde_json::to_string(&response).unwrap());

// Error response
let error_response = JSendResponse::error("An error occurred".to_string(), Some(100), None::<String>);
println!("{}", serde_json::to_string(&error_response).unwrap());

A more in-depth example of how this crate could be used with a framework like axum can be found in the examples/ directory.

Features

Dependencies

~0.3–1MB
~23K SLoC