Skip to content

Conversation

@wanderingnish
Copy link

Summary

Fix inconsistent Player serialization in team lists - objects now serialize at their outermost layer only

Problem

The serialize() function mixed integers and full Player dicts in team lists:

"team": [1, {"number": 3, "name": "PlayerThree", ...}, 5, 7]  

Expected:

"team": [1, 3, 5, 7]  

Root Cause

When serializing a list, objects from later in the list were serialized WITHIN earlier objects.

Example: In player list [P1, P2, P3, P4], P1 has {team: [P1, P3]}. When serializing P1's team, P3 would be fully serialized instead of using a hash value and letting P3 be serialized later in the outer list.

Solution

Two-pass approach for lists:

  1. First pass: Mark all objects as seen (reserve for this layer)
  2. Second pass: Recursively serialize each object; inner references to objects from
    outer layer will now use hash value.

Each object is fully serialized exactly once at its outermost layer.

Testing

Added 3 new tests in tests/test_serialize.py:

  • test_players_are_dicts: Players serialize as full dict objects
  • test_team_references_are_integers: Team lists contain only integers
  • test_no_circular_references: No circular references in serialized data

All 24 tests pass (21 existing + 3 new)

Impact

  • ✅ Consistent integer references across all contexts
  • ✅ ~385KB smaller JSON output for 8-player games
  • ✅ No breaking changes to public API

🤖 Initially generated with Claude Code, edited & verified manually.

Team lists often contained a mix of integers and full Player objects

Example: In a player list [P1, P2, P3, P4], P1 has attribute {team: [P1, P3]}.
When serializing P1's team, P3 would be fully serialized as a dict instead of
waiting for P3 to be serialized outermost layer.

Solution: Two-pass approach for lists
- First pass: Mark all objects in the list as seen (reserving them for this layer)
- Second pass: Recursively serialize each object; inner references to objects from
outer layer will now use hash value.

Testing: Added 3 new tests (tests/test_serialize.py)
- test_players_are_dicts: Players serialize as full dict objects
- test_team_references_are_integers: Team lists contain only integers
- test_no_circular_references: No circular references in serialized data
- All 24 tests pass (21 existing + 3 new)

Co-Authored-By: Claude <[email protected]>
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.

1 participant