bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays#3034
bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays#3034onealj wants to merge 3 commits into
Conversation
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. These changes are contributed under the Python Software License 2.0 or similar. I can sign a contributor license agreement if needed.
|
Hello, and thanks for your contribution! I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA). Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue. Thanks again to your contribution and we look forward to looking at it! |
onealj
left a comment
There was a problem hiding this comment.
The user may want to sort sets and frozensets so that the json output is identical for the same input.
|
I'd be happy to write some unit tests for the new functionality. Let me know if it adding this capability to the json module is of interest to the PSF. |
If the user passes in an iterator to a dict rather than the dict itself, handle the special dict_keys, dict_values, or dict_items iterators for serialization as a json array (perhaps the dict_itemiterator should write out a json dictionary instead).
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_typesnonlocal 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