#[derive(Default)] struct JsonByteCounter(usize); impl std::io::Write for JsonByteCounter { fn write(&mut self, bytes: &[u8]) -> std::io::Result { self.0 = self.0.saturating_add(bytes.len()); Ok(bytes.len()) } fn flush(&mut self) -> std::io::Result<()> { Ok(()) } } /// Counts serialized JSON bytes without retaining the serialized output. pub(crate) fn serialized_json_bytes( value: &T, ) -> serde_json::Result { let mut counter = JsonByteCounter::default(); serde_json::to_writer(&mut counter, value)?; Ok(counter.0) }