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

jsend

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

3 stable releases

1.0.2 Mar 16, 2024

#18 in #api-response

Download history 286/week @ 2024-03-15 26/week @ 2024-03-22 45/week @ 2024-03-29 15/week @ 2024-04-05

60 downloads per month

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.4–1.1MB
~26K SLoC