Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions Lib/json/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion Lib/mimetypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading