Custom derive macros for JSON serialization with pretty and simple formatting options.
This crate provides four derive macros that implement Debug
and Display
traits using JSON serialization:
-
DebugPretty
: ImplementsDebug
with pretty-printed JSON output -
DisplayPretty
: ImplementsDisplay
with pretty-printed JSON output -
DebugSimple
: ImplementsDebug
with compact JSON output -
DisplaySimple
: ImplementsDisplay
with compact JSON output
Add this to your Cargo.toml
:
[dependencies]
pretty-simple-display = "0.1.0"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::Serialize;
#[derive(Serialize, DebugPretty, DisplaySimple)]
struct User {
id: u64,
name: String,
email: String,
}
let user = User {
id: 1,
name: "Alice".to_string(),
email: "alice@example.com".to_string(),
};
// Pretty-printed JSON via Debug
format!("{:?}", user);
// Compact JSON via Display
format!("{}", user);
Pretty formatting (using DebugPretty
or DisplayPretty
):
{
"id": 1,
"name": "Alice",
"email": "alice@example.com"
}
Simple formatting (using DebugSimple
or DisplaySimple
):
{"id":1,"name":"Alice","email":"alice@example.com"}
use pretty_simple_display::{DebugPretty, DisplaySimple};
use serde::Serialize;
#[derive(Serialize, DebugPretty, DisplaySimple)]
struct Product {
id: u32,
name: String,
price: f64,
}
let product = Product {
id: 123,
name: "Widget".to_string(),
price: 29.99,
};
// Debug uses pretty formatting
println!("{:?}", product);
// Output:
// {
// "id": 123,
// "name": "Widget",
// "price": 29.99
// }
// Display uses simple formatting
println!("{}", product);
// Output: {"id":123,"name":"Widget","price":29.99}
use pretty_simple_display::DebugPretty;
use serde::Serialize;
#[derive(Serialize, DebugPretty)]
enum Status {
Active,
Inactive,
Pending { reason: String },
}
let status1 = Status::Active;
let status2 = Status::Pending { reason: "Verification".to_string() };
println!("{:?}", status1); // "Active"
println!("{:?}", status2);
// {
// "Pending": {
// "reason": "Verification"
// }
// }
use pretty_simple_display::DisplaySimple;
use serde::Serialize;
#[derive(Serialize)]
struct Address {
street: String,
city: String,
}
#[derive(Serialize, DisplaySimple)]
struct Person {
name: String,
address: Address,
tags: Vec<String>,
}
let person = Person {
name: "John".to_string(),
address: Address {
street: "123 Main St".to_string(),
city: "Anytown".to_string(),
},
tags: vec!["developer".to_string(), "rust".to_string()],
};
println!("{}", person);
// {"name":"John","address":{"street":"123 Main St","city":"Anytown"},"tags":["developer","rust"]}
All macros handle serialization errors gracefully by displaying an error message instead of panicking:
Error serializing to JSON: <error_details>
- Your struct must implement
serde::Serialize
- The
serde
crate must be available in your dependencies - Compatible with all types that serde can serialize (structs, enums, primitives, collections, etc.)
Derive Macro | Trait | Format | Use Case |
---|---|---|---|
DebugPretty |
Debug |
Pretty JSON | Development, debugging, logs |
DisplayPretty |
Display |
Pretty JSON | User-facing output, formatted display |
DebugSimple |
Debug |
Compact JSON | Performance-critical debugging |
DisplaySimple |
Display |
Compact JSON | APIs, compact serialization |
We welcome contributions to this project! If you would like to contribute, please follow these steps:
- Fork the repository.
- Create a new branch for your feature or bug fix.
- Make your changes and ensure that the project still builds and all tests pass.
- Commit your changes and push your branch to your forked repository.
- Submit a pull request to the main repository.
If you have any questions, issues, or would like to provide feedback, please feel free to contact the project maintainer:
Joaquín Béjar García
- Email: jb@taunais.com
- GitHub: joaquinbejar
We appreciate your interest and look forward to your contributions!
Licensed under MIT license