pub fn json<F>(f: F) -> impl DisplayJson + DisplayExpand description
Similiar to Json, but can be used for pretty-printing and in-place JSON generation purposes.
§Examples
§Basic usage
use nojson::json;
// Standard JSON serialization (compact)
let compact = json(|f| f.value([1, 2, 3]));
assert_eq!(compact.to_string(), "[1,2,3]");§Pretty printing with custom indentation
use nojson::json;
// Pretty-printed JSON with 2-space indentation
let pretty = json(|f| {
f.set_indent_size(2);
f.set_spacing(true);
f.value([1, 2, 3])
});
assert_eq!(
format!("\n{}", pretty),
r#"
[
1,
2,
3
]"#
);§Mixing formatting styles
use nojson::{json, DisplayJson};
// You can nest formatters with different settings
let mixed = json(|f| {
f.set_indent_size(2);
f.set_spacing(true);
f.value([
&vec![1] as &dyn DisplayJson,
&json(|f| {
f.set_indent_size(0);
f.value(vec![2, 3])
}),
])
});
assert_eq!(
format!("\n{}", mixed),
r#"
[
[
1
],
[2, 3]
]"#
);