From 743b9314b3fc9212f5cb211ebcb16a015d5b1afd Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sun, 25 Oct 2026 23:58:30 -0500 Subject: [PATCH] fix(json): restore the escaped replacement for invalid UTF-8 (#17 was wrong) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit PR #17 changed the invalid-byte branch from jsonuesc to jsonrune and said the change matched Go. It did not. The check behind #17 encoded string([]byte{0x8b}), and that conversion replaces the invalid byte with a valid U+FFFD scalar before encoding, so it measured the valid-scalar path and never the invalid one. Measured with the pinned toolchain, one byte 0x8b held verbatim in a string: GOTOOLCHAIN=go1.26.8 go run -> 22 5c 75 66 66 66 64 22 ("�") go1.27.3 (jsonv2 default) -> 22 ef bf bd 22 (raw EF BF BD) The json suite builds its reference with go1.26.8 on purpose (see the comment on GOTOOLCHAIN in om/json/test/run.py), and #17 made the encoder disagree with it: 12 byte-quote rows failed, one per offending byte 0x80-0x8b. Restore jsonuesc in the invalid-byte branch. The else branch keeps jsonrune, so a valid U+FFFD scalar is still written as its raw UTF-8 bytes. The README sentence now states both halves and names the pinned encoder. Evidence: sh om/json/test/run.sh -> PASS, 7706 lines compared, 0 mismatches; sh tools/test.sh -> 58 PASS, 0 FAIL (was 57/1 with #17). Co-Authored-By: Claude Fable 5.1 Co-Authored-By: Drake Stapleton --- om/json/README.md | 2 +- om/json/json.om | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/om/json/README.md b/om/json/README.md index e5f1a19..c1d8fdd 100644 --- a/om/json/README.md +++ b/om/json/README.md @@ -92,7 +92,7 @@ syntax state: punctuation/field sequencing is the caller's responsibility. canonicalization scheme. Output can exceed the input size and fail explicitly. String output matches Go `json.Marshal(string)`: standard short control escapes, lowercase `\u` -hex, HTML escapes for `<`, `>` and `&`, and escapes for U+2028/U+2029. Invalid UTF-8 becomes the replacement character as raw UTF-8 bytes; a valid + hex, HTML escapes for `<`, `>`, and `&`, and escapes for U+2028/U+2029. Invalid UTF-8 becomes the six-character escape `\ufffd`, matching the Go 1.26 encoder the test corpus is generated with; a valid U+FFFD scalar is written as its raw UTF-8 bytes. U+FFFD scalar is written the same way, matching Go. Internal helpers all have the `json` prefix; callers should use the APIs above. diff --git a/om/json/json.om b/om/json/json.om index 1038cec..9812919 100644 --- a/om/json/json.om +++ b/om/json/json.om @@ -650,7 +650,7 @@ p = jsonuesc o p v end else if and eq v 65533 eq get r 1 1 -p = jsonrune o p v +p = jsonuesc o p v else p = jsonrune o p v end