[issue34972] json dump silently converts int keys to string
Change by My-Tien Nguyen : -- nosy: My-Tien Nguyen priority: normal severity: normal status: open title: json dump silently converts int keys to string ___ Python tracker <https://bugs.python.org/issue34972> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34972] json dump silently converts int keys to string
New submission from My-Tien Nguyen : When int keys are silently converted to string on json serialization, the user needs to remember to convert it back to int on loading. I think that a warning should be shown at least. In my case I serialize a dict to json with int keys, later load it back into a dict (resulting in a dict with string keys) and test for existence of an int key in the dict which will then return False incorrectly. I am aware that json does not support int keys, but this can be easily forgotten. -- components: +Library (Lib) type: -> behavior versions: +Python 3.6 ___ Python tracker <https://bugs.python.org/issue34972> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34972] json dump silently converts int keys to string
My-Tien Nguyen added the comment: I don’t think, “other languages do that too” is a good argument here. This would apply if behaving differently would break user expectation. But here we would do nothing more than explicitly inform the user of a relevant operation. If they already expected that behaviour, they can disregard the warning. I don’t see how `parse_int`would help me here, I would need a `parse_str=int`, but then it would try to parse every string, and I don’t see the use case for that. I would suggest a warning similar to this: --- json/encoder.py +++ json/encoder.py @@ -1,6 +1,7 @@ """Implementation of JSONEncoder """ import re +import warnings try: from _json import encode_basestring_ascii as c_encode_basestring_ascii @@ -353,7 +354,9 @@ items = sorted(dct.items(), key=lambda kv: kv[0]) else: items = dct.items() +non_str_key = False for key, value in items: +non_str_key = non_str_key or not isinstance(key, str) if isinstance(key, str): pass # JavaScript is weakly typed for these, so it makes sense to @@ -403,6 +406,8 @@ else: chunks = _iterencode(value, _current_indent_level) yield from chunks +if non_str_key: +warnings.warn("Encountered non-string key(s), converted to string.", RuntimeWarning) if newline_indent is not None: _current_indent_level -= 1 yield '\n' + _indent * _current_indent_level -- ___ Python tracker <https://bugs.python.org/issue34972> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34972] json dump silently converts int keys to string
My-Tien Nguyen added the comment: Sure, I can do that, but wanted to propose this regardless. I guess this is a disagreement on a language design level. As a proponent of strong typing I wouldn’t have allowed non-string keys in the first place, and if they are allowed I would warn about conversion. This is also more aligned with the “explicit is better than implicit” principle. -- resolution: not a bug -> status: closed -> open ___ Python tracker <https://bugs.python.org/issue34972> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com
[issue34972] json dump silently converts int keys to string
Change by My-Tien Nguyen : -- status: open -> closed ___ Python tracker <https://bugs.python.org/issue34972> ___ ___ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com