use crate::error::Result;
use serde::Serialize;
pub struct JsonOutput {
pretty: bool,
}
impl JsonOutput {
pub fn new(pretty: bool) -> Self {
Self { pretty }
}
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(())
}
pub fn print_jsonl<T: Serialize>(&self, value: &T) -> Result<()> {
let json = serde_json::to_string(value)?;
println!("{json}");
Ok(())
}
pub fn print_lines<T: Serialize>(&self, values: &[T]) -> Result<()> {
for value in values {
self.print_jsonl(value)?;
}
Ok(())
}
}