Skip to content

Fix TypeError when dumping events containing bytes as JSON - #111

Open
ZayanKhan-12 wants to merge 1 commit into
Blizzard:masterfrom
ZayanKhan-12:fix-91-json-bytes
Open

Fix TypeError when dumping events containing bytes as JSON#111
ZayanKhan-12 wants to merge 1 commit into
Blizzard:masterfrom
ZayanKhan-12:fix-91-json-bytes

Conversation

@ZayanKhan-12

Copy link
Copy Markdown

Fixes #91

Problem

On Python 3 the decoders produce bytes for blob/string fields, and compat.json_dumps passes the event straight to json.dumps, which raises:

TypeError: Object of type bytes is not JSON serializable

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_dumps now recursively decodes bytes — including dict keys, and values nested in lists/tuples — using the encoding argument it already receives, which is exactly what json.dumps(obj, encoding=...) did on Python 2, so Python 3 output matches the legacy behavior. The iso-8859-1 encoding the CLI passes decodes every possible byte value, so the conversion never raises.

Verification

  • Before the fix: json_dumps({'m_name': b'Alarak'}, encoding='iso-8859-1') reproduces the exact TypeError from the issue
  • New tests/test_compat.py (pytest, 2 tests) covers bytes values, bytes dict keys, nesting in lists/tuples, and all 256 byte values round-tripping
  • CI checks run locally on the changed files: black -S --check clean, codespell clean, flake8 --select=E9,F63,F7,F82 clean

🤖 Generated with Claude Code

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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

TypeError in python -m heroprotocol --initdata --json ...

1 participant