fix(json): tolerate raw control chars in strings instead of dropping output#2536
Open
dosthcpp wants to merge 1 commit into
Open
fix(json): tolerate raw control chars in strings instead of dropping output#2536dosthcpp wants to merge 1 commit into
dosthcpp wants to merge 1 commit into
Conversation
…output `rtk json` (and any pipe like `curl ... | rtk json`) called strict `serde_json::from_str`, which rejects unescaped control characters (U+0000–U+001F) inside string values per RFC 8259 §7. Some real-world producers emit them anyway (e.g. an API echoing a user-supplied newline verbatim into a field). When that happened rtk printed nothing and exited 1, losing the entire payload and forcing the user to re-fetch with a raw passthrough — a net-negative outcome for a token-saving tool, and a violation of the "Never Block" design principle. Add `parse_json_lenient`: parse strictly first (fast path, zero behavior change for valid JSON), and only on failure retry once with raw in-string control characters rewritten to their equivalent `\uXXXX` escapes via a string-aware scanner. Whitespace between tokens and already-escaped sequences are left byte-for-byte identical, and genuinely malformed input still surfaces the original strict error. Adds 9 unit tests (recovery in both compact and schema modes, control char in keys, fast-path borrow, whitespace preservation, existing-escape preservation, and malformed-still-errors). Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
rtk json(and pipes likecurl … | rtk json) used strictserde_json::from_str, which rejects unescaped control characters (U+0000–U+001F) inside string values per RFC 8259 §7. Some real-world producers emit them anyway (e.g. an API echoing a user-supplied newline into a field). rtk then printed nothing and exited 1, losing the whole payload and forcing a raw re-fetch — net-negative for a token-saving tool and contrary to the Never Block principle.parse_json_lenient: strict parse first (fast path, zero behavior change for valid JSON), and only on failure retry once with raw in-string control chars rewritten to their equivalent\uXXXXescapes via a string-aware scanner. Whitespace between tokens and already-escaped sequences stay byte-for-byte identical; genuinely malformed input still surfaces the original strict error.Before / After
Test plan
cargo test json_cmd— 19 passed, 0 failed (9 new + 10 existing). New tests cover recovery in compact and schema modes, control char in keys, fast-path borrow, whitespace preservation, existing-escape preservation, and malformed-still-errors.cargo fmt -- --check src/cmds/system/json_cmd.rs— cleancargo clippy --lib— no new warnings on the changed fileprintf '{"id":"abc","body":"a\nb"}' | rtk json -now renders the payload (exit 0);printf '{not json' | rtk json -still errors (exit 1); valid JSON output unchanged.