Fix TypeError when dumping events containing bytes as JSON - #111
Open
ZayanKhan-12 wants to merge 1 commit into
Open
Fix TypeError when dumping events containing bytes as JSON#111ZayanKhan-12 wants to merge 1 commit into
ZayanKhan-12 wants to merge 1 commit into
Conversation
On Python 3 the decoders produce bytes for blob and string fields, and json.dumps raises "TypeError: Object of type bytes is not JSON serializable" for any event containing them, breaking e.g. `python -m heroprotocol --initdata --json <replay>`. Recursively decode bytes (including dict keys and values nested in lists and tuples) using the encoding json_dumps already receives, matching what json.dumps(obj, encoding=...) did on Python 2. The iso-8859-1 encoding used by the CLI decodes every byte value, so this never raises. Fixes Blizzard#91 Co-Authored-By: Claude Fable 5 <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.
Fixes #91
Problem
On Python 3 the decoders produce
bytesfor blob/string fields, andcompat.json_dumpspasses the event straight tojson.dumps, which raises:for any command that hits such an event — e.g.
python -m heroprotocol --initdata --json <replay>, as reported (and reproduced here with a bytes-containing event before the fix).Fix
json_dumpsnow recursively decodesbytes— including dict keys, and values nested in lists/tuples — using theencodingargument it already receives, which is exactly whatjson.dumps(obj, encoding=...)did on Python 2, so Python 3 output matches the legacy behavior. Theiso-8859-1encoding the CLI passes decodes every possible byte value, so the conversion never raises.Verification
json_dumps({'m_name': b'Alarak'}, encoding='iso-8859-1')reproduces the exact TypeError from the issuetests/test_compat.py(pytest, 2 tests) covers bytes values, bytes dict keys, nesting in lists/tuples, and all 256 byte values round-trippingblack -S --checkclean,codespellclean,flake8 --select=E9,F63,F7,F82clean🤖 Generated with Claude Code