Michael Smith <[email protected]> added the comment:
OK, I appreciate the response. The MappingProxy objects I am working with are
in a nested data structure, so accessing them to coerce them directly isn't
feasible. I created
class MappingProxyEncoder(JSONEncoder):
def default(self, obj):
if isinstance(obj, MappingProxyType):
return obj.copy()
return JSONEncoder.default(self, obj)
and using that works fine for json.dumps (passes all my tests, at least). But I
suspect that obj.copy() is more expensive than the code I'm replacing, which is
a local implementation of ImmutableDict I was trying to do away with, following
a readthrough of PEP 416. I wonder if there's a cheaper way to get at the
underlying dictionary.
I suspect I'm not the only python user who foolishly expected MappingProxy to
be a drop-in replacement for local ImmutableDict implementations. Maybe this
ticket will be useful for others to read.
----------
_______________________________________
Python tracker <[email protected]>
<https://bugs.python.org/issue34858>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com