8000 Privatize font_manager.JSONEncoder. by anntzer · Pull Request #15082 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Privatize font_manager.JSONEncoder. #15082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 23, 2019
Merged
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
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/2019-08-18-AL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Deprecations
````````````

``font_manager.JSONEncoder`` is deprecated. Use `.font_manager.json_dump` to
dump a `.FontManager` instance.
2 changes: 1 addition & 1 deletion doc/missing-references.json
Original file line number Diff line number Diff line change
Expand Up @@ -3504,7 +3504,7 @@
"matplotlib.dviread.Font": [
"<unknown>:1"
],
"json.encoder.JSONEncoder": [
"matplotlib.font_manager._JSONEncoder": [
"lib/matplotlib/font_manager.py:docstring of matplotlib.font_manager.JSONEncoder:1"
],
"font_manager.FontProperties": [
Expand Down
31 changes: 21 additions & 10 deletions lib/matplotlib/font_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -858,7 +858,7 @@ def copy(self):
return new


class JSONEncoder(json.JSONEncoder):
class _JSONEncoder(json.JSONEncoder):
def default(self, o):
if isinstance(o, FontManager):
return dict(o.__dict__, __class__='FontManager')
Expand All @@ -876,6 +876,11 @@ def default(self, o):
return super().default(o)


@cbook.deprecated("3.2", alternative="json_dump")
class JSONEncoder(_JSONEncoder):
pass


def _json_decode(o):
cls = o.pop('__class__', None)
if cls is None:
Expand All @@ -896,26 +901,32 @@ def _json_decode(o):

def json_dump(data, filename):
"""
Dumps a data structure as JSON in the named file.
Dump `FontManager` *data* as JSON to the file named *filename*.

Notes
-----
File paths that are children of the Matplotlib data path (typically, fonts
shipped with Matplotlib) are stored relative to that data path (to remain
valid across virtualenvs).

Handles FontManager and its fields. File paths that are children of the
Matplotlib data path (typically, fonts shipped with Matplotlib) are stored
relative to that data path (to remain valid across virtualenvs).
See Also
--------
json_load
"""
with open(filename, 'w') as fh:
try:
json.dump(data, fh, cls=JSONEncoder, indent=2)
json.dump(data, fh, cls=_JSONEncoder, indent=2)
except OSError as e:
_log.warning('Could not save font_manager cache {}'.format(e))


def json_load(filename):
"""
Loads a data structure as JSON from the named file.
Load a `FontManager` from the JSON file named *filename*.

Handles FontManager and its fields. Relative file paths are interpreted
as being relative to the Matplotlib data path, and transformed into
absolute paths.
See Also
--------
json_dump
"""
with open(filename, 'r') as fh:
return json.load(fh, object_hook=_json_decode)
Expand Down
0