serde_json_wrapper 0.1.0

Serialize T as a pretty JSON string
Documentation
  • Coverage
  • 50%
    1 out of 2 items documented0 out of 1 items with examples
  • Size
  • Source code size: 10.22 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 1.5 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 25s Average build duration of successful builds.
  • all releases: 25s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • JohnScience/serde_json_wrapper
    0 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • JohnScience

serde_json_wrapper

Crates.io Downloads Documentation License Dependency Status

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.

Example

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);
}