1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
//! Pretty-printing JSONs with ease.
use anyhow::Result;
use serde::Serialize;
use serde_json::{ser::PrettyFormatter, Serializer};
use crate::INDENT;
/// Pretty-print a serializable value as JSON.
///
/// ```rust
/// use jsonutils::print::print_json;
/// use serde_json::json;
///
/// let json = json!({
/// "name": "Alice",
/// "age": 30,
/// "is_student": false
/// });
///
/// print_json(json).unwrap();
/// ```
pub fn print_json<T: Serialize>(value: T) -> Result<()> {
let mut buf = Vec::new();
let fmtr = PrettyFormatter::with_indent(INDENT);
let mut ser = Serializer::with_formatter(&mut buf, fmtr);
value.serialize(&mut ser)?;
println!("{}", String::from_utf8(buf)?);
Ok(())
}
/// Pretty-print a serializable value as JSON with a custom indentation.
///
/// ```rust
/// use jsonutils::print::print_json_with_indent;
/// use serde_json::json;
///
/// let json = json!({
/// "name": "Alice",
/// "age": 30,
/// "is_student": false
/// });
///
/// print_json_with_indent(json, 2).unwrap();
/// ```
pub fn print_json_with_indent<T: Serialize>(value: T, indent: usize) -> Result<()> {
let indent = " ".repeat(indent);
let indent = indent.as_bytes();
let mut buf = Vec::new();
let fmtr = PrettyFormatter::with_indent(indent);
let mut ser = Serializer::with_formatter(&mut buf, fmtr);
value.serialize(&mut ser)?;
println!("{}", String::from_utf8(buf)?);
Ok(())
}