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.

Author july
Recipients docs@python, july
Date 2013-08-23.10:30:29
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1377253830.03.0.463497944995.issue18820@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2013-08-23 10:30:30julysetrecipients: + july, docs@python
2013-08-23 10:30:30julysetmessageid: <1377253830.03.0.463497944995.issue18820@psf.upfronthosting.co.za>
2013-08-23 10:30:29julylinkissue18820 messages
2013-08-23 10:30:29julycreate