On Thu, Sep 17, 2020 at 6:54 AM Wes Turner <wes.tur...@gmail.com> wrote:
>
>
> Why would we impose UTF-8 when the spec says UTF-8, UTF-16, or UTF-32?

Obsolete JSON spec said UTF-8, UTF-16, and UTF-32. Current spec says UTF-8.
See https://tools.ietf.org/html/rfc8259#section-8.1

So `dumpf` must use UTF-8, although `loadf` can support UTF-16 and
UTF-32 like `loads`.

>
> How could this be improved? (I'm on my phone, so)
>
> def dumpf(obj, path, *args, **kwargs):
>     with open(getattr(path, '__path__', path), 'w', 
> encoding=kwargs.get('encoding', 'utf8')) as _file:
>         return dump(_file, *args, **kwargs)
>
> def loadf(obj, path, *args, **kwargs):
>     with open(getattr(path, '__path__', path), 
> encoding=kwargs.get('encoding', 'utf8')) as _file:
>         return load(_file, *args, **kwargs)
>

def dumpf(obj, path, *, **kwargs):
    with open(path, "w", encoding="utf-8") as f:
        return dump(obj, f, **kwargs)

def loadf(obj, path, *, **kwargs):
    with open(path, "rb") as f:
        return load(f, **kwargs)

Regards,
-- 
Inada Naoki  <songofaca...@gmail.com>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/5RPHOVBMAC3USBKY7S2G4WVEC4JR4IV6/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to