On Mon, Mar 27, 2017 at 4:27 PM, Wes Turner <wes.tur...@gmail.com> wrote:

>
>
> On Mon, Mar 27, 2017 at 10:34 AM, Chris Barker <chris.bar...@noaa.gov>
> wrote:
>
>> On Mon, Mar 27, 2017 at 7:59 AM, Paul Moore <p.f.mo...@gmail.com> wrote:
>>
>>> On 27 March 2017 at 15:40, Ram Rachum <r...@rachum.com> wrote:
>>> > Another idea: Maybe make json.load and json.dump support Path objects?
>>>
>>> If they currently supported filenames, I'd say that's a reasonable
>>> extension. Given that they don't, it still seems like more effort than
>>> it's worth to save a few characters
>>>
>>
>> Sure, but they probably should -- it's a REALLY common (most common)
>> use-case to read and write JSON from a file. And many APIs support
>> "filename or open file-like object".
>>
>> I'd love to see that added, and, or course, support for Path objects as
>> well.
>>
>
>
>

>
>
> https://docs.python.org/2/library/json.html#encoders-and-decoders
>
> # https://docs.python.org/2/library/json.html#json.JSONEncoder
>
> class PathJSONEncoder(json.JSONEncoder):
>     def default(self, obj):
>         if isinstance(obj, pathlib.Path):
>             return unicode(obj)  # ? (what about bytes)
>             return OrderedDict((
>                 ('@type', 'pydatatypes:pathlib.Path'),  # JSON-LD
>                 ('path', unicode(obj)), )
>          return json.JSONEncoder.default(self, obj)
>
>
> # https://docs.python.org/2/library/json.html#json.JSONDecoder
> def as_pathlib_Path(obj):
>         if obj.get('@type') == 'pydatatypes:pathlib.Path':
>             return pathlib.Path(obj.get('path'))
>         return obj
>

def as_pathlib_Path(obj):
        if hasattr(obj, 'get') and obj.get('@type') ==
'pydatatypes:pathlib.Path':
            return pathlib.Path(obj.get('path'))
        return obj


>
>
> def read_json(self, **kwargs):
>     object_pairs_hook = kwargs.pop('object_pairs_hook',
> collections.OrderedDict) # OrderedDefaultDict
>     object_hook = kwargs.pop('object_hook', as_pathlib_Path)
>     encoding = kwargs.pop('encoding', 'utf8')
>     with codecs.open(self, 'r ', encoding=encoding) as _file:
>         return json.load(_file,
>             object_pairs_hook=object_pairs_hook,
>             object_hook=object_hook,
>             **kwargs)
>
> def write_json(self, obj, **kwargs):
>     kwargs['cls'] = kwargs.pop('cls', PathJSONEncoder)
>     encoding = kwargs.pop('encoding', 'utf8')
>     with codecs.open(self, 'w', encoding=encoding) as _file:
>         return json.dump(obj, _file, **kwargs)
>
>
> def test_pathlib_json_encoder_decoder():
>     p = pathlib.Path('./test.json')
>     obj = dict(path=p, _path=str(unicode(p)))
>     p.write_json(obj)
>     obj2 = p.read_json()
>     assert obj['path'] == obj2['path']
>     assert isinstance(obj['path'], pathlib.Path)
>

should it be 'self' or 'obj'?


>
>
>
> https://github.com/jaraco/path.py/blob/master/path.py#L735
> open()
> bytes()
> chunks()
> write_bytes()
> text()
> def write_text(self, text, encoding=None, errors='strict',
>                    linesep=os.linesep, append=False):
> lines()
> write_lines()
>
> read_hash()
> read_md5()
> read_hexhash()
>
>
>
>
>>
>> -CHB
>>
>>
>>
>>
>> --
>>
>> 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.bar...@noaa.gov
>>
>> _______________________________________________
>> Python-ideas mailing list
>> Python-ideas@python.org
>> https://mail.python.org/mailman/listinfo/python-ideas
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>>
>
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to