netpulse-cli 0.1.1

A zero-config, single-binary network quality monitor with percentile stats, jitter, and MTR-style traceroute
Documentation
// src/export/json.rs — JSON Streaming Exporter
//
// Writes StatsSnapshot entries as newline-delimited JSON (NDJSON) to stdout.
// This format is ideal for piping into tools like `jq`, `grep`, logging
// aggregators, or any streaming JSON consumer.
//
// Example output line:
// {"target":"8.8.8.8","sample_count":60,"loss_pct":0.0,"rtt_p50_us":12340,...}

use crate::stats::StatsSnapshot;
use anyhow::Result;
use std::io::{self, Write};

/// Exporter that writes StatsSnapshots as NDJSON to stdout.
pub struct JsonExporter {
    /// If true, emit each individual ProbeResult in addition to summaries.
    /// Use for real-time streaming; disable for periodic summary mode.
    pretty: bool,
}

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

    /// Write a single StatsSnapshot as one JSON line to stdout.
    pub fn emit(&self, snapshot: &StatsSnapshot) -> Result<()> {
        let stdout = io::stdout();
        let mut handle = stdout.lock();

        let json = if self.pretty {
            serde_json::to_string_pretty(snapshot)?
        } else {
            serde_json::to_string(snapshot)?
        };

        writeln!(handle, "{}", json)?;
        handle.flush()?;
        Ok(())
    }
}