//! JSON output formatting

use crate::error::Result;
use serde::Serialize;

/// JSON output formatter
pub struct JsonOutput {
    pretty: bool,
}

impl JsonOutput {
    pub fn new(pretty: bool) -> Self {
        Self { pretty }
    }

    /// Print a value as JSON
    pub fn print<T: Serialize + ?Sized>(&self, value: &T) -> Result<()> {
        let json = if self.pretty {
            serde_json::to_string_pretty(value)?
        } else {
            serde_json::to_string(value)?
        };
        println!("{json}");
        Ok(())
    }

    /// Print a value as JSONL (one JSON object per line)
    pub fn print_jsonl<T: Serialize>(&self, value: &T) -> Result<()> {
        let json = serde_json::to_string(value)?;
        println!("{json}");
        Ok(())
    }

    /// Print multiple values as JSONL
    pub fn print_lines<T: Serialize>(&self, values: &[T]) -> Result<()> {
        for value in values {
            self.print_jsonl(value)?;
        }
        Ok(())
    }
}
