[docs] [issue18820] json.dump() ignores its 'default' option when serializing dictionary keys

July Tikhonov report at bugs.python.org
Fri Aug 23 12:30:30 CEST 2013

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)
>>> 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 at python
components: Documentation, Library (Lib)
files: json-default-dict-keys.diff
keywords: patch
messages: 195957
nosy: docs at 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 at bugs.python.org>

More information about the docs mailing list