pretty-simple-display
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 outputDisplayPretty
: ImplementsDisplay
with pretty-printed JSON outputDebugSimple
: ImplementsDebug
with compact JSON outputDisplaySimple
: ImplementsDisplay
with compact JSON output
Quick Start
Add this to your Cargo.toml
:
[]
= "0.1.0"
= { = "1.0", = ["derive"] }
= "1.0"
Basic Usage
use ;
use Serialize;
let user = User ;
// Pretty-printed JSON via Debug
format!;
// Compact JSON via Display
format!;
Output Comparison
Pretty vs Simple Formatting
Pretty formatting (using DebugPretty
or DisplayPretty
):
Simple formatting (using DebugSimple
or DisplaySimple
):
Advanced Usage
Multiple Derives on Same Struct
use ;
use Serialize;
let product = Product ;
// Debug uses pretty formatting
println!;
// Output:
// {
// "id": 123,
// "name": "Widget",
// "price": 29.99
// }
// Display uses simple formatting
println!;
// Output: {"id":123,"name":"Widget","price":29.99}
Working with Enums
use DebugPretty;
use Serialize;
let status1 = Active;
let status2 = Pending ;
println!; // "Active"
println!;
// {
// "Pending": {
// "reason": "Verification"
// }
// }
Nested Structures
use DisplaySimple;
use Serialize;
let person = Person ;
println!;
// {"name":"John","address":{"street":"123 Main St","city":"Anytown"},"tags":["developer","rust"]}
Error Handling
All macros handle serialization errors gracefully by displaying an error message instead of panicking:
Error serializing to JSON: <error_details>
Requirements
- 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.)
Feature Comparison
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 |
Contribution and Contact
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!
✍️ License
Licensed under MIT license