Skip to content

JSON library unable to provide index during parse operations. #135354

Closed as not planned
@smaudet

Description

@smaudet

Feature or enhancement

Proposal:

import json

def some_hook_logic(pairs: []):
    # some logic here

if __name__ == '__main__':
    with open('some.json', 'r') as fp:
        json.load(fp, object_pairs_hook=some_hook_logic)

Currently hooks are provided to allow for some custom processing of json files. However, there is no way to obtain the index of the hooked operation. This would be especially useful for providing validation feedback on files, but there may be other benefits to providing this information to the hooks.

JSONDecoder has a raw_decode method on it that can be overwritten, which could be passed the index, however currently this parameter is essentially unused, and the index is available only to the scanner implementation (which can either be pure python or the _json.c module.

The proposal/feature is to add the index as a parameter to the hooks, or make it available as a part of JSONDecoder, possibly as new index-aware hooks so as to leave existing code interoperable.

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    extension-modulesC modules in the Modules dirstdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    Done

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions