Skip to main content

torvyn_cli/output/
json.rs

1//! JSON output rendering.
2//!
3//! Emits structured JSON to stdout. Used when `--format json` is specified.
4
5use serde::Serialize;
6
7/// Print a serializable value as JSON to stdout.
8///
9/// COLD PATH — called once per command.
10pub fn print_json<T: Serialize>(value: &T) {
11    match serde_json::to_string_pretty(value) {
12        Ok(json_str) => {
13            println!("{json_str}");
14        }
15        Err(e) => {
16            eprintln!("error: Failed to serialize output to JSON: {e}");
17            println!(r#"{{"error": "serialization_failed", "detail": "{}"}}"#, e);
18        }
19    }
20}
21
22/// Print a value as NDJSON (newline-delimited JSON).
23///
24/// COLD PATH — called per progress event during long operations.
25#[allow(dead_code)]
26pub fn print_ndjson<T: Serialize>(value: &T) {
27    match serde_json::to_string(value) {
28        Ok(json_str) => {
29            println!("{json_str}");
30        }
31        Err(e) => {
32            eprintln!("error: Failed to serialize event to JSON: {e}");
33        }
34    }
35}