Skip to content

Fix JSON loading on Python 3: keep strings as str in convertJsonUnicode#1245

Open
xairaven wants to merge 1 commit into
mininet:masterfrom
xairaven:miniedit-serializing-fix
Open

Fix JSON loading on Python 3: keep strings as str in convertJsonUnicode#1245
xairaven wants to merge 1 commit into
mininet:masterfrom
xairaven:miniedit-serializing-fix

Conversation

@xairaven

Copy link
Copy Markdown

Summary

Fixes a bug in convertJsonUnicode that encoded JSON strings to bytes on Python 3. That conversion caused json.dumps() to fail when saving topologies (TypeError: bytes not JSON serializable), which in turn produced empty .mn files and runtime errors such as KeyError: 'hosts' during load/save cycles.

Root cause

The previous implementation converted all unicode strings to byte strings unconditionally. On Python 3, str is the native text type and should not be encoded to bytes. Encoding resulted in non-serializable values inside the topology dictionary, breaking saveTopology.

Changes

  • Update convertJsonUnicode to:
    • Recursively traverse dicts and lists as before.
    • Only encode unicode -> bytes when running under Python 2.
    • Return str values unchanged on Python 3.
  • No other behavior changed; topology structure is preserved.

Impact

  • Fixes cases where loading a topology then saving it produced an empty file.
  • Prevents TypeError from json.dumps() and the downstream KeyError: 'hosts' observed during loadTopology.
  • Maintains backwards compatibility with Python 2 (still encodes unicode to utf-8 bytes when sys.version_info[0] == 2).

How to test

  1. Start MiniEdit and open an existing .mn topology (example JSON attached in issue).

  2. Verify the loaded values are str on Python 3:

    type(app.appPrefs['ipBase'])  # should be <class 'str'>

    Save the topology and confirm the .mn file is non-empty and contains expected keys (hosts, switches, controllers, links).

    Basic smoke test: create topology in GUI, save, reopen, and check that nodes/links persist.

Notes

A temporary fallback in saveTopology (e.g. json.dumps(..., default=str)) is not included here because it masks the root cause. This PR fixes the root cause so that serialization proceeds normally.

If desired, we can add a small unit test for convertJsonUnicode to assert that strings remain str on Python 3.

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