Skip to main content

push_json_string

Function push_json_string 

Source
pub fn push_json_string(output: &mut String, value: &str)
Expand description

Append value to output as one quoted JSON string literal. 把 value 作为一个带引号的 JSON 字符串字面量追加到 output。

§Why the obvious implementation is wrong

为什么直白写法是错的

value.replace('\\', "\\\\").replace('"', "\\\"") is the shape a reader reaches for first, and it is wrong in a way this workspace’s own tests cannot see: a tab or a newline inside a name is emitted raw, which makes the record invalid for every conforming reader, while the lenient parser in this crate still accepts it. A round-trip test therefore passes on invalid output. That is exactly what happened to MirGraph::to_jsonl, and it is why the pinning test below asserts the absence of raw control characters rather than a round trip. value.replace('\\', "\\\\").replace('"', "\\\"") 是读者最先想到的写法,而它的错处 恰是本工作区自己的测试看不见的:名字里的制表符或换行会被原样写出,使记录对任何合规读取器 非法,而本 crate 的宽松解析器仍然接受它。于是往返测试会在非法输出上通过——MirGraph::to_jsonl 正是如此,这也是下面的钉子测试断言“不存在原样控制字符”而不是做往返的原因。

§Boundary

边界

The escape set is exactly RFC 8259’s: the quotation mark, the reverse solidus, and U+0000–U+001F (the short form where JSON defines one, \u00XX otherwise, so even a NUL is representable). Nothing else is escaped, so non-ASCII text stays readable in the artifact. 转义集恰好是 RFC 8259 的那一份:引号、反斜杠、以及 U+0000–U+001F(JSON 定义了短形式 的用短形式,其余用 \u00XX,因此连 NUL 也能表示)。其余字符一律不转义,非 ASCII 文本 因此在工件里保持可读。