Crates.io | serde_json_wrapper |
lib.rs | serde_json_wrapper |
version | 0.1.0 |
created_at | 2025-06-28 23:58:29.690588+00 |
updated_at | 2025-06-28 23:58:29.690588+00 |
description | Serialize T as a pretty JSON string |
homepage | |
repository | https://github.com/JohnScience/serde_json_wrapper |
max_upload_size | |
id | 1730210 |
size | 10,709 |
This is a simple library that provides a JsonPretty<T>
wrapper around any type T
that implements serde::Serialize
and/or serde::Deserialize
. It allows you to serialize and deserialize T
as a pretty JSON string (see serde_json::to_string_pretty
).
One notable use case is to use it for rendering the JSON representation of T
with handlebars::Handlebars::render
.
use serde::{Deserialize, Serialize};
use serde_json_wrapper::JsonPretty;
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
struct TestStruct {
field1: String,
field2: i32,
}
fn main() {
let test_data = TestStruct {
field1: "value1".to_string(),
field2: 42,
};
let json_pretty = JsonPretty(test_data);
let json_value = serde_json::to_value(&json_pretty).unwrap();
let serde_json::Value::String(serialized) = json_value else {
panic!("Expected a JSON string");
};
let expected = "{\n \"field1\": \"value1\",\n \"field2\": 42\n}";
assert_eq!(serialized, expected);
}