jsonxf

Function pretty_print

Source
pub fn pretty_print(json_string: &str) -> Result<String, String>
Expand description

Pretty-prints a string of JSON-encoded data.

Input must be valid JSON data in UTF-8 encoding.

The output will use two spaces as an indent, a line feed as newline character, and no trailing whitespace. To customize this behavior, use a jsonxf::Formatter::pretty_printer() directly.

ยงExamples:

assert_eq!(
    jsonxf::pretty_print("{\"a\":1,\"b\":2}").unwrap(),
    "{\n  \"a\": 1,\n  \"b\": 2\n}"
);
assert_eq!(
    jsonxf::pretty_print("{\"empty\":{},\n\n\n\n\n\"one\":[1]}").unwrap(),
    "{\n  \"empty\": {},\n  \"one\": [\n    1\n  ]\n}"
);