@@ -1386,20 +1386,38 @@ def read_block(self, fn, offset, length, delimiter=None):
13861386 length = size - offset
13871387 return read_block (f , offset , length , delimiter )
13881388
1389- def to_json (self ) -> str :
1389+ def to_json (self , * , include_password : bool = True ) -> str :
13901390 """
13911391 JSON representation of this filesystem instance.
13921392
1393+ Parameters
1394+ ----------
1395+ include_password: bool, default True
1396+ Whether to include the password (if any) in the output.
1397+
13931398 Returns
13941399 -------
13951400 JSON string with keys ``cls`` (the python location of this class),
13961401 protocol (text name of this class's protocol, first one in case of
13971402 multiple), ``args`` (positional args, usually empty), and all other
13981403 keyword arguments as their own keys.
1404+
1405+ Warnings
1406+ --------
1407+ Serialized filesystems may contain sensitive information which have been
1408+ passed to the constructor, such as passwords and tokens. Make sure you
1409+ store and send them in a secure environment!
13991410 """
14001411 from .json import FilesystemJSONEncoder
14011412
1402- return json .dumps (self , cls = FilesystemJSONEncoder )
1413+ return json .dumps (
1414+ self ,
1415+ cls = type (
1416+ "_FilesystemJSONEncoder" ,
1417+ (FilesystemJSONEncoder ,),
1418+ {"include_password" : include_password },
1419+ ),
1420+ )
14031421
14041422 @staticmethod
14051423 def from_json (blob : str ) -> AbstractFileSystem :
@@ -1426,25 +1444,40 @@ def from_json(blob: str) -> AbstractFileSystem:
14261444
14271445 return json .loads (blob , cls = FilesystemJSONDecoder )
14281446
1429- def to_dict (self ) -> Dict [str , Any ]:
1447+ def to_dict (self , * , include_password : bool = True ) -> Dict [str , Any ]:
14301448 """
14311449 JSON-serializable dictionary representation of this filesystem instance.
14321450
1451+ Parameters
1452+ ----------
1453+ include_password: bool, default True
1454+ Whether to include the password (if any) in the output.
1455+
14331456 Returns
14341457 -------
14351458 Dictionary with keys ``cls`` (the python location of this class),
14361459 protocol (text name of this class's protocol, first one in case of
14371460 multiple), ``args`` (positional args, usually empty), and all other
14381461 keyword arguments as their own keys.
1462+
1463+ Warnings
1464+ --------
1465+ Serialized filesystems may contain sensitive information which have been
1466+ passed to the constructor, such as passwords and tokens. Make sure you
1467+ store and send them in a secure environment!
14391468 """
14401469 cls = type (self )
14411470 proto = self .protocol
14421471
1472+ storage_options = dict (self .storage_options )
1473+ if not include_password :
1474+ storage_options .pop ("password" , None )
1475+
14431476 return dict (
14441477 cls = f"{ cls .__module__ } :{ cls .__name__ } " ,
14451478 protocol = proto [0 ] if isinstance (proto , (tuple , list )) else proto ,
14461479 args = self .storage_args ,
1447- ** self . storage_options ,
1480+ ** storage_options ,
14481481 )
14491482
14501483 @staticmethod
0 commit comments