This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author varunagrawal
Recipients varunagrawal
Date 2019-07-18.05:03:15
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1563426195.91.0.749324402695.issue37615@roundup.psfhosted.org>
In-reply-to
Content
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.
History
Date User Action Args
2019-07-18 05:03:15varunagrawalsetrecipients: + varunagrawal
2019-07-18 05:03:15varunagrawalsetmessageid: <1563426195.91.0.749324402695.issue37615@roundup.psfhosted.org>
2019-07-18 05:03:15varunagrawallinkissue37615 messages
2019-07-18 05:03:15varunagrawalcreate