diff --git a/Lib/json/__init__.py b/Lib/json/__init__.py index 1d972d22ded072..428ab2098c1727 100644 --- a/Lib/json/__init__.py +++ b/Lib/json/__init__.py @@ -121,7 +121,7 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw): """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a - ``.write()``-supporting file-like object). + ``.write()``-supporting file-like object or file path). If ``skipkeys`` is true then ``dict`` keys that are not basic types (``str``, ``int``, ``float``, ``bool``, ``None``) will be skipped @@ -174,10 +174,17 @@ def dump(obj, fp, *, skipkeys=False, ensure_ascii=True, check_circular=True, check_circular=check_circular, allow_nan=allow_nan, indent=indent, separators=separators, default=default, sort_keys=sort_keys, **kw).iterencode(obj) + + if type(fp) == str: + fp = open(fp, "w") + # could accelerate with writelines in some versions of Python, at # a debuggability cost for chunk in iterable: fp.write(chunk) + + if type(fp) == str: + fp.close() def dumps(obj, *, skipkeys=False, ensure_ascii=True, check_circular=True, @@ -273,7 +280,7 @@ def detect_encoding(b): def load(fp, *, cls=None, object_hook=None, parse_float=None, parse_int=None, parse_constant=None, object_pairs_hook=None, **kw): - """Deserialize ``fp`` (a ``.read()``-supporting file-like object containing + """Deserialize ``fp`` (a ``.read()``-supporting file-like object or fiel path containing a JSON document) to a Python object. ``object_hook`` is an optional function that will be called with the @@ -290,10 +297,18 @@ def load(fp, *, cls=None, object_hook=None, parse_float=None, To use a custom ``JSONDecoder`` subclass, specify it with the ``cls`` kwarg; otherwise ``JSONDecoder`` is used. """ - return loads(fp.read(), + if type(fp) == str: + fp = open(fp) + + data = loads(fp.read(), cls=cls, object_hook=object_hook, parse_float=parse_float, parse_int=parse_int, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw) + + if type(fp) == str: + fp.close() + + return data def loads(s, *, cls=None, object_hook=None, parse_float=None, diff --git a/Lib/mimetypes.py b/Lib/mimetypes.py index d7c4e8444f8dec..857e89c14dc4e7 100644 --- a/Lib/mimetypes.py +++ b/Lib/mimetypes.py @@ -441,7 +441,7 @@ def _default_mime_types(): '.Z': 'compress', '.bz2': 'bzip2', '.xz': 'xz', - '.br': 'br', + '.br': 'br' } # Before adding new types, make sure they are either registered with IANA,