bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays by onealj · Pull Request #3034 · python/cpython (original) (raw)
Based on a discussion on Stack Overflow
https://stackoverflow.com/questions/45457946/python-efficient-reverse-list-json-serialisation/45458128#45458128
I wrote out a solution that is general enough to support most of core Python.
If there are performance issues with the iterable_types
nonlocal lookup, these could be pulled in as local variables to the inner _iterencode_*
functions.
As written, a user would have to monkey patch json.encoder._get_iterable_types()
(or one of the functions it calls) if they wanted to be able to encode their own sequence-like containers or iterators as a json array.
If there are plans for the json library to sequence-like classes and iterators that are part of the core language, part of the standard library, and in third party modules, then some modifications will be needed so that monkey patching isn't necessary.