//! Shared JSON serialization helper.
//!
//! All `tidalctl` JSON flows through this single `serde_json`-backed path, so
//! control characters, unicode, and quotes in any string field (paths, error
//! messages, reasons) are always escaped correctly — there is no hand-rolled
//! escaping left to get wrong.

use serde::Serialize;

use crate::CliError;

/// Serialize a value to JSON, compact or pretty.
///
/// # Errors
///
/// Returns [`CliError`] if serialization fails. Serialization of these plain
/// data structs is infallible in practice (no maps with non-string keys, no
/// custom `Serialize` that can error), so this is a defensive boundary rather
/// than an expected path.
pub(crate) fn render_json<T: Serialize>(value: &T, pretty: bool) -> Result<String, CliError> {
    let result = if pretty {
        serde_json::to_string_pretty(value)
    } else {
        serde_json::to_string(value)
    };
    result.map_err(|e| CliError::new(format!("internal json error: {e}")))
}
