bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays #3034
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
https://bugs.python.org/issue31155