Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: 'int' object is not iterable #32

Closed
eevleevs opened this issue Jan 26, 2020 · 7 comments
Closed

TypeError: 'int' object is not iterable #32

eevleevs opened this issue Jan 26, 2020 · 7 comments

Comments

@eevleevs
Copy link

eevleevs commented Jan 26, 2020

pyjson5 0.8.5 on python 3.6.3

>>> 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
@dpranke
Copy link
Owner

dpranke commented 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
Copy link
Author

Should be fine to do the same as the json module. But there is a possibility to introduce duplicate keys:

>>> json.dumps({1:2,'1':3})
'{"1": 3, "1": 2}'
>>> json.loads(json.dumps({1:2,'1':3}))
{'1': 2}

Maybe it could be worth to have a warning in such a case?

Same story goes for floats.

@dpranke
Copy link
Owner

dpranke commented 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
Copy link
Owner

dpranke commented 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
Copy link
Author

Great 👍

@dpranke
Copy link
Owner

dpranke commented Jan 31, 2020

Okay, this should be fixed now in v0.9.0, which is live on PyPI.

@eevleevs
Copy link
Author

Confirmed, thanks.

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

No branches or pull requests

2 participants