[issue35111] Make Custom Object Classes JSON Serializable

Bob Ippolito report at bugs.python.org
Wed Oct 31 02:17:23 EDT 2018


Bob Ippolito <bob at redivi.com> added the comment:

The trouble with having such a hook is that it would take precedence over any customization you might want or need to do to satisfy the protocol you're implementing. Other than the limited set of types that are part of the JSON specification, there's essentially no standard for encoding of anything else. This is why customization is left to the call sites for encoding and decoding, and I would recommend using the `default` and `object_pairs_hook` keyword arguments to `dumps` and `loads` respectively for that, rather than any of the options that you've enumerated.

For what it's worth, simplejson has had a `for_json` method hook for several years to encourage some consolidation, but it's opt-in and there hasn't been a lot of demand to make it the default. The inverse `from_json` type operation is not supported. If you think about it, how *could* you even implement such a thing in the general case, in a way that wouldn't have lots of surprises and performance issues?

----------

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


More information about the Python-bugs-list mailing list