This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: json.dump() ignores its 'default' option when serializing dictionary keys
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.11, Python 3.10, Python 3.9
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Erwin Mayer, berker.peksag, bob.ippolito, cvrebert, docs@python, ezio.melotti, iritkatriel, july, pitrou, rhettinger, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2013-08-23 10:30 by july, last changed 2022-04-11 14:57 by admin.

File name Uploaded Description Edit
json-default-dict-keys.diff july, 2013-08-23 10:58 applies to default, does not fix C implementation review
json-default-tests.diff july, 2013-08-23 21:37 proposed tests (for default) review
Messages (8)
msg195957 - (view) Author: July Tikhonov (july) * Date: 2013-08-23 10:30
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.
msg195958 - (view) Author: July Tikhonov (july) * Date: 2013-08-23 10:58
Oops, my patch disables 'skipkeys' argument of dump. Another version attached.
msg196022 - (view) Author: Ezio Melotti (ezio.melotti) * (Python committer) Date: 2013-08-23 20:32
Are there already tests that cover the changes in your patch?  If not, could you add them?
msg196036 - (view) Author: July Tikhonov (july) * Date: 2013-08-23 21:37
Proposed tests attached.
msg263013 - (view) Author: Erwin Mayer (Erwin Mayer) Date: 2016-04-08 09:56
Will this be merged? I also believe it is an unexpected behavior to not serialize dictionary keys when the default option is used.
msg263019 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2016-04-08 11:01
See also

And allowing non-string keys leads to other issue:

Thus there is an argument for disallowing serializing non-string keys.
msg263025 - (view) Author: Erwin Mayer (Erwin Mayer) Date: 2016-04-08 12:41
Regarding the issues mentioned in, they already apply with the current implementation anyway (true is serialized as 'true'), so users must already be careful.

The JSONEncoder with default parameters could definitely keep rejecting complex keys, but an optional 'encode_complex_keys=False' parameter could be added to __init__ to provide this functionality. There would be zero performance impact as the parameter check would only be done if all else has failed instead of raising the TypeError (the same way _skipkeys does).

In that respect, the patch of this issue would need to be amended (and the C version would also need to be changed).

I am trying to fork the json core module to achieve this (to not have to wait for the next Python release, assuming it gets implemented), if you are familiar with the packaging process of core modules into standalone modules, your advice would be much appreciated (and could well help others contribute to Python):
msg415093 - (view) Author: Irit Katriel (iritkatriel) * (Python committer) Date: 2022-03-13 20:16
Reproduced on 3.11.
Date User Action Args
2022-04-11 14:57:49adminsetgithub: 63020
2022-03-13 20:16:36iritkatrielsetnosy: + iritkatriel

messages: + msg415093
versions: + Python 3.9, Python 3.10, Python 3.11, - Python 3.5, Python 3.6
2021-12-30 01:13:58andrei.avklinkissue41569 superseder
2016-04-08 12:41:34Erwin Mayersetmessages: + msg263025
2016-04-08 11:01:10serhiy.storchakasetnosy: + bob.ippolito, serhiy.storchaka
messages: + msg263019
2016-04-08 10:02:16berker.peksagsetassignee: docs@python ->
components: - Documentation
versions: - Python 3.4
2016-04-08 09:56:53Erwin Mayersetnosy: + Erwin Mayer
messages: + msg263013
2015-10-22 17:42:44berker.peksagsetnosy: + berker.peksag
stage: test needed -> patch review
type: enhancement -> behavior

versions: + Python 3.5, Python 3.6
2014-05-16 04:31:09cvrebertsetnosy: + cvrebert
2013-08-23 21:37:12julysetfiles: + json-default-tests.diff

messages: + msg196036
2013-08-23 20:32:50ezio.melottisetnosy: + rhettinger, pitrou, ezio.melotti

messages: + msg196022
stage: test needed
2013-08-23 10:59:26julysetfiles: - json-default-dict-keys.diff
2013-08-23 10:58:37julysetfiles: + json-default-dict-keys.diff

messages: + msg195958
2013-08-23 10:30:30julycreate