Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c634e63

Browse files
authored
Merge pull request #15082 from anntzer/jsonencoder
Privatize font_manager.JSONEncoder.
2 parents f0c2c6d + 937ba83 commit c634e63

File tree

3 files changed

+27
-11
lines changed

3 files changed

+27
-11
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Deprecations
2+
````````````
3+
4+
``font_manager.JSONEncoder`` is deprecated. Use `.font_manager.json_dump` to
5+
dump a `.FontManager` instance.

doc/missing-references.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3365,7 +3365,7 @@
33653365
"matplotlib.dviread.Font": [
33663366
"<unknown>:1"
33673367
],
3368-
"json.encoder.JSONEncoder": [
3368+
"matplotlib.font_manager._JSONEncoder": [
33693369
"lib/matplotlib/font_manager.py:docstring of matplotlib.font_manager.JSONEncoder:1"
33703370
],
33713371
"font_manager.FontProperties": [

lib/matplotlib/font_manager.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,7 @@ def copy(self):
859859
return new
860860

861861

862-
class JSONEncoder(json.JSONEncoder):
862+
class _JSONEncoder(json.JSONEncoder):
863863
def default(self, o):
864864
if isinstance(o, FontManager):
865865
return dict(o.__dict__, __class__='FontManager')
@@ -877,6 +877,11 @@ def default(self, o):
877877
return super().default(o)
878878

879879

880+
@cbook.deprecated("3.2", alternative="json_dump")
881+
class JSONEncoder(_JSONEncoder):
882+
pass
883+
884+
880885
def _json_decode(o):
881886
cls = o.pop('__class__', None)
882887
if cls is None:
@@ -897,26 +902,32 @@ def _json_decode(o):
897902

898903
def json_dump(data, filename):
899904
"""
900-
Dumps a data structure as JSON in the named file.
905+
Dump `FontManager` *data* as JSON to the file named *filename*.
906+
907+
Notes
908+
-----
909+
File paths that are children of the Matplotlib data path (typically, fonts
910+
shipped with Matplotlib) are stored relative to that data path (to remain
911+
valid across virtualenvs).
901912
902-
Handles FontManager and its fields. File paths that are children of the
903-
Matplotlib data path (typically, fonts shipped with Matplotlib) are stored
904-
relative to that data path (to remain valid across virtualenvs).
913+
See Also
914+
--------
915+
json_load
905916
"""
906917
with open(filename, 'w') as fh:
907918
try:
908-
json.dump(data, fh, cls=JSONEncoder, indent=2)
919+
json.dump(data, fh, cls=_JSONEncoder, indent=2)
909920
except OSError as e:
910921
_log.warning('Could not save font_manager cache {}'.format(e))
911922

912923

913924
def json_load(filename):
914925
"""
915-
Loads a data structure as JSON from the named file.
926+
Load a `FontManager` from the JSON file named *filename*.
916927
917-
Handles FontManager and its fields. Relative file paths are interpreted
918-
as being relative to the Matplotlib data path, and transformed into
919-
absolute paths.
928+
See Also
929+
--------
930+
json_dump
920931
"""
921932
with open(filename, 'r') as fh:
922933
return json.load(fh, object_hook=_json_decode)

0 commit comments

Comments
 (0)