[issue34858] MappingProxy objects should JSON serialize just like a dictionary

Raymond Hettinger report at bugs.python.org
Sun Sep 30 23:26:23 EDT 2018


Raymond Hettinger <raymond.hettinger at gmail.com> added the comment:

IIRC, there was an early decision to handle only exact types and their subclasses (plus tuples).  For example, os.environ and instances of collections.ChainMap are not JSON serializable.

I believe the reason was that it made encoding faster, more predictable, and more likely to match round-trip expectations.  For those wanting more generality, there are at least two options.  The simplest option is to coerce the input to supported type.  A more complete solution is to write a subclass of JSONEncoder to pass into json.dump() as the *cls* argument (there are examples of how to do this in the docs).

For the specific case of mappingproxy, there is another issue.  Multiple components of a class dict are not all JSON serializable, so you have the same problem yet again with getset_descriptor objects, member objects, and various slot wrappers.

----------
assignee:  -> bob.ippolito
nosy: +bob.ippolito, rhettinger
versions:  -Python 3.4, Python 3.5, Python 3.6, Python 3.7

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue34858>
_______________________________________


More information about the Python-bugs-list mailing list