coding_tools/jsonout.rs
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Jonathan Shook
3
4//! Shared JSON result output for the `--json` / `--json-pretty` tools: one
5//! place that decides compact (the default, one line for piping into `jq`) vs.
6//! pretty-printed (indented, for reading). `--json-pretty` implies `--json`.
7
8use serde_json::Value;
9
10/// Print a JSON value to stdout — pretty-printed (indented) when `pretty`, else
11/// compact on a single line.
12pub fn print(value: &Value, pretty: bool) {
13 if pretty {
14 println!(
15 "{}",
16 serde_json::to_string_pretty(value).expect("a JSON value serializes")
17 );
18 } else {
19 println!("{value}");
20 }
21}