Move JSON byte counting into codex-utils-string (#61465)

Export `serialized_json_bytes` from `codex-utils-string` and update core callers to use the shared helper. Preserve byte counting without retaining the serialized output.

GitOrigin-RevId: 9c75b05592d3171b9a0d54517f8f3a4281fde987
This commit is contained in:
Ruslan Nigmatullin
2026-11-14 05:00:21 +00:00
committed by copyberry
parent ecf54824c6
commit 336b19ab26
7 changed files with 26 additions and 27 deletions
+22 -1
View File
@@ -1,9 +1,30 @@
//! JSON serialization helpers for ASCII-only transports and bounded output.
//! JSON serialization helpers for counting bytes, bounding output, and ASCII-only transports.
use std::io;
use serde::Serialize;
#[derive(Default)]
struct JsonByteCounter(usize);
impl io::Write for JsonByteCounter {
fn write(&mut self, bytes: &[u8]) -> io::Result<usize> {
self.0 = self.0.saturating_add(bytes.len());
Ok(bytes.len())
}
fn flush(&mut self) -> io::Result<()> {
Ok(())
}
}
/// Counts serialized JSON bytes without retaining the serialized output.
pub fn serialized_json_bytes<T: Serialize + ?Sized>(value: &T) -> serde_json::Result<usize> {
let mut counter = JsonByteCounter::default();
serde_json::to_writer(&mut counter, value)?;
Ok(counter.0)
}
struct AsciiJsonFormatter;
impl serde_json::ser::Formatter for AsciiJsonFormatter {
+1
View File
@@ -1,6 +1,7 @@
mod json;
mod truncate;
pub use json::serialized_json_bytes;
pub use json::to_ascii_json_string;
pub use json::to_json_string_bounded;
pub use truncate::TextTruncation;