You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
>>> d={'u':{1:2,3:4},'b':4}
>>> json.dumps(d)
'{"u": {"1": 2, "3": 4}, "b": 4}'
>>> json5.dumps(d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 168, in dumps
**kwargs)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 249, in _dumps
**kwargs)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 283, in _dump_dict
s += key_str + kv_sep + _dumps(obj[key], seen, **kwargs)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 249, in _dumps
**kwargs)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 279, in _dump_dict
valid_key, key_str = _dumpkey(key, ensure_ascii, quote_keys)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 311, in _dumpkey
return True, _dump_str(k, ensure_ascii)
File "/usr/local/lib/python3.6/dist-packages/json5/lib.py", line 317, in _dump_str
for ch in obj:
TypeError: 'int' object is not iterable
Good point. The Python JSON module documentation explicitly notes that "loads(dumps(x)) != x if x has non-string keys." I can at least add the same note to my documentation, but I probably wouldn't have a warning if the json module doesn't.
Activity
dpranke commentedon Jan 28, 2020
Thanks for the bug report.
Interesting; integer keys aren't actually legal in JSON5 (or JSON), but this is about what's supported in the host language, not JSON5.
If you are okay with the integers being converted to strings (as the json module does), it seems fine to implement this.
eevleevs commentedon Jan 28, 2020
Should be fine to do the same as the json module. But there is a possibility to introduce duplicate keys:
Maybe it could be worth to have a warning in such a case?
Same story goes for floats.
dpranke commentedon Jan 29, 2020
Good point. The Python JSON module documentation explicitly notes that "loads(dumps(x)) != x if x has non-string keys." I can at least add the same note to my documentation, but I probably wouldn't have a warning if the json module doesn't.
dpranke commentedon Jan 29, 2020
I've got a fix for this now, I'm trying to wrap up fixes for the other open bugs and get a release out, probably tomorrow or the next day.
eevleevs commentedon Jan 29, 2020
Great 👍
dpranke commentedon Jan 31, 2020
Okay, this should be fixed now in v0.9.0, which is live on PyPI.
eevleevs commentedon Jan 31, 2020
Confirmed, thanks.