Skip to content

Support the buffer protocol in json.loads() #74379

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

Open
fafhrd91 opened this issue Apr 27, 2017 · 7 comments
Open

Support the buffer protocol in json.loads() #74379

fafhrd91 opened this issue Apr 27, 2017 · 7 comments
Assignees
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@fafhrd91
Copy link
Contributor

fafhrd91 commented Apr 27, 2017

BPO 30193
Nosy @rhettinger, @etrepum, @ezio-melotti, @serhiy-storchaka, @fafhrd91
PRs
  • bpo-30193: Allow to load buffer objects with json.loads() #1334
  • bpo-30193: Allow to load buffer objects with json.loads() #14977
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/etrepum'
    closed_at = None
    created_at = <Date 2017-04-27.22:55:44.853>
    labels = ['3.7', 'type-feature', 'library']
    title = 'Support the buffer protocol in json.loads()'
    updated_at = <Date 2019-07-27.09:55:07.816>
    user = 'https://github.com/fafhrd91'

    bugs.python.org fields:

    activity = <Date 2019-07-27.09:55:07.816>
    actor = 'flavianhautbois'
    assignee = 'bob.ippolito'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2017-04-27.22:55:44.853>
    creator = 'fafhrd91'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 30193
    keywords = ['patch']
    message_count = 5.0
    messages = ['292487', '292508', '292509', '293250', '293251']
    nosy_count = 5.0
    nosy_names = ['rhettinger', 'bob.ippolito', 'ezio.melotti', 'serhiy.storchaka', 'fafhrd91']
    pr_nums = ['1334', '14977']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue30193'
    versions = ['Python 3.7']

    Linked PRs

    @fafhrd91
    Copy link
    Contributor Author

    It is not possible to use buffer objects in json.loads()

    @fafhrd91 fafhrd91 added the 3.7 (EOL) end of life label Apr 27, 2017
    @vstinner vstinner changed the title Allow to load buffer objects with json.loads() Accept memoryview in json.loads() Apr 28, 2017
    @serhiy-storchaka
    Copy link
    Member

    This isn't so easy. Supporting the buffer protocol can slowdown the common case -- str.

    Do you have concrete use case for this feature or add it just as an extension of bytes and bytearray support?

    @serhiy-storchaka serhiy-storchaka added the stdlib Python modules in the Lib dir label Apr 28, 2017
    @serhiy-storchaka serhiy-storchaka changed the title Accept memoryview in json.loads() Support the buffer protocol in json.loads() Apr 28, 2017
    @serhiy-storchaka serhiy-storchaka added the type-feature A feature request or enhancement label Apr 28, 2017
    @serhiy-storchaka
    Copy link
    Member

    Withdraw my note about slowing down str.

    @serhiy-storchaka
    Copy link
    Member

    The proposed patch doesn't work with all objects supporting the buffer protocol, for example it doesn't work with array.array('u', '\ufeff[1,2,3]'). This is easy to fix. But I'm not sure that it is worth to complicate the code for adding support of the buffer protocol.

    @fafhrd91
    Copy link
    Contributor Author

    fafhrd91 commented May 8, 2017

    I am fine with any decision. close with "won't fix" is fine too

    @kovidgoyal
    Copy link

    Please re-open this. There is currently no way to decode JSON in the stdlib from a memoryview. In my case the memoryview comes from C code, copying it into a temporary bytes object just because json.loads() does not support the buffer protocol is very wasteful and has a significant performance impact for large amounts of JSON data.

    Tangentially, the type annotations for json.loads() dont raise an error if you pass in a memoryview into it.

    @iritkatriel iritkatriel reopened this Nov 7, 2023
    @AlexWaygood
    Copy link
    Member

    AlexWaygood commented Nov 7, 2023

    Tangentially, the type annotations for json.loads() dont raise an error if you pass in a memoryview into it.

    Please open an issue at https://github.com/python/typeshed if you want that fixed @kovidgoyal — typeshed deals with all the type annotations for the stdlib!

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Status: Done
    Development

    No branches or pull requests

    6 participants