Skip to main content

prettify_with

Function prettify_with 

Source
pub fn prettify_with<O: Options>(input: &str) -> Result<String>
Expand description

prettify under an explicit write policy.

The policy decides the layout exactly as it does when a value is written: INDENT sets the width and NEW_LINES_IN_ARRAYS decides whether an array gets a line per element.

use structio::{PrettyInlineArrays, json::prettify_with};

let out = prettify_with::<PrettyInlineArrays>(r#"{"v":[1,2,3]}"#).unwrap();
assert_eq!(out, "{\n  \"v\": [1, 2, 3]\n}");

PRETTY is honoured too, rather than assumed, so a compact policy compacts. That makes Standard a minifier, which is the same walk with nothing to emit between tokens:

use structio::{Standard, json::prettify_with};

let out = prettify_with::<Standard>("{\n  \"a\": [1, 2]\n}").unwrap();
assert_eq!(out, r#"{"a":[1,2]}"#);

minify reaches the same bytes on any document that is really JSON, and much faster, because compacting needs none of the structure this walks. Reach for that unless you want the walk’s checking too.

The reading settings do not apply, there being no schema here to have an opinion about: an unknown key is every key. The exception is ALLOW_COMMENTS, which decides whether a document may carry comments at all. It drops them, as everything in this crate does, a comment being no part of what a writer can emit.