New submission from July Tikhonov: According to documentation of json.dump(), concerning its 'default' option: default(obj) is a function that should return a serializable version of obj or raise TypeError. The default simply raises TypeError. But this function is actually never applied to serialized dictionary keys:
def default(obj): ... if isinstance(obj, bytes): ... return obj.decode('ascii') ... raise ValueError ... json.dumps(b'asdf') Traceback (most recent call last): ... TypeError: b'asdf' is not JSON serializable json.dumps(b'asdf', default=default) '"asdf"' json.dumps({b'asdf' : 1}, default=default) Traceback (most recent call last): ... TypeError: keys must be a string json.dumps({1 : b'asdf'}, default=default) '{"1": "asdf"}'
(bytes are used purely for the purpose of example) Such behavior should be either documented or corrected. Patch correcting python implementation of json attached. ---------- assignee: docs@python components: Documentation, Library (Lib) files: json-default-dict-keys.diff keywords: patch messages: 195957 nosy: docs@python, july priority: normal severity: normal status: open title: json.dump() ignores its 'default' option when serializing dictionary keys type: enhancement versions: Python 3.4 Added file: http://bugs.python.org/file31436/json-default-dict-keys.diff _______________________________________ Python tracker <report@bugs.python.org> <http://bugs.python.org/issue18820> _______________________________________