Skip to content

TypeError: 'int' object is not iterable #32

Closed

Description

@eevleevs

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

Activity

  1. dpranke commented on Jan 28, 2020

    @dpranke
    Owner

    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.

  2. eevleevs commented on Jan 28, 2020

    @eevleevs
    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.

  3. dpranke commented on Jan 29, 2020

    @dpranke
    Owner

    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.

  4. dpranke commented on Jan 29, 2020

    @dpranke
    Owner

    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.

  5. eevleevs commented on Jan 29, 2020

    @eevleevs
    Author

    Great 👍

  6. dpranke commented on Jan 31, 2020

    @dpranke
    Owner

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

  7. eevleevs commented on Jan 31, 2020

    @eevleevs
    Author

    Confirmed, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions