[Python-ideas] Add pathlib.Path.write_json and pathlib.Path.read_json

Chris Barker chris.barker at noaa.gov
Mon Mar 27 16:33:11 EDT 2017


On Mon, Mar 27, 2017 at 9:43 AM, Bruce Leban <bruce at leban.us> wrote:

> I'm not in favor of this idea for the reason mentioned by many of the
> other posters. BUT ... this does bring up something missing from json
> readers: *the ability to read one json object from the input rather than
> reading the entire input* and attempting to interpret it as one object.
>

I can't tell from the JSON spec (at least not quickly), but it is possible
to have more than one object at the top level?

Experimenting with the python json module seems to indicate that it is not
-- you can only have one "thing" in a JSON file -- either an "object" or an
array.

then, of course you can arbitrarily nest stuff inside that top-level
container.

Since the nesting is arbitrary, I'm not sure it's clear how a
one-object-at-a-time reader would work in the general case?

-CHB



> For my use case, it would be sufficient to read whole lines only but I can
> imagine other use cases.
>
> The basic rule would be to read as much of the input as necessary (and no
> more) to read a single json object, ignoring leading white space.
>
> In practical terms:
>
>    - if the first character is [ or { or " read to the matching ] or } or
>    "
>    - otherwise if the first character is a digit or '-' read as many
>    characters as possible to parse a number
>    - otherwise attempt to match 'true', 'false' or 'null'
>    - otherwise fail
>
>
> --- Bruce
> Check out my puzzle book and get it free here:
> http://J.mp/ingToConclusionsFree (available on iOS)
>
>
>
> On Mon, Mar 27, 2017 at 5:50 AM, Ram Rachum <ram at rachum.com> wrote:
>
>> Hi guys,
>>
>> What do you think about adding methods pathlib.Path.write_json and
>> pathlib.Path.read_json , similar to write_text, write_bytes, read_text,
>> read_bytes?
>>
>> This would make writing / reading JSON to a file a one liner instead of a
>> two-line with clause.
>>
>>
>> Thanks,
>> Ram.
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas at python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/
>
>


-- 

Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R            (206) 526-6959   voice
7600 Sand Point Way NE   (206) 526-6329   fax
Seattle, WA  98115       (206) 526-6317   main reception

Chris.Barker at noaa.gov
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170327/91fea94b/attachment.html>


More information about the Python-ideas mailing list