New submission from Varun Agrawal <varagra...@gmail.com>:
Currently, json.load and json.dump take file pointers, aka file-like objects which have support for `.write()` and `.read()`. This necessitates the following pattern of code on a regular basis: ``` with open(filename) as f: o = json.load(f) ... with open(filename, 'w') as f: json.dump(o, f) ``` While this is a fair pattern to live by, it would be nice for programmers to pass in the filename directly and let the functions handle opening and closing the file. The way to do this would be to have a simple check if `fp` is a path-like object or a file-like object, and set up a context manager accordingly (using contextlib). The original `load()` and `dump()` code definition can then be placed inside the context manager, which should make testing painless. There might be an argument for this not being in alignment with duck-typing, but the overall benefits of this approach along with the ease afforded to programmers should make for a strong argument. ---------- components: Library (Lib) messages: 348097 nosy: varunagrawal priority: normal severity: normal status: open title: json - make load and dump work with filenames and path-like objects type: enhancement versions: Python 3.9 _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue37615> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com