Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays #3034

Closed
wants to merge 3 commits into from

Conversation

onealj
Copy link

@onealj onealj commented Aug 9, 2017

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

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.
@the-knights-who-say-ni
Copy link

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!

Copy link
Author

@onealj onealj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user may want to sort sets and frozensets so that the json output is identical for the same input.

@onealj onealj changed the title Encode set, frozenset, bytearray, and iterators as json arrays bpo-31155: Encode set, frozenset, bytearray, and iterators as json arrays Aug 9, 2017
@onealj
Copy link
Author

onealj commented Aug 9, 2017

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).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants