@@ -561,7 +561,12 @@ def json_description(self) -> str:
561
561
"""
562
562
return self .__class__ .__doc__ or "Generated with PyCardano"
563
563
564
- def to_json (self , key_type : Optional [str ] = None , description : Optional [str ] = None ) -> str :
564
+ def to_json (
565
+ self ,
566
+ key_type : Optional [str ] = None ,
567
+ description : Optional [str ] = None ,
568
+ ** kwargs ,
569
+ ) -> str :
565
570
"""
566
571
Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex.
567
572
@@ -570,17 +575,21 @@ def to_json(self, key_type: Optional[str] = None, description: Optional[str] = N
570
575
Args:
571
576
key_type (str): The type to use in the JSON output. Defaults to the class name.
572
577
description (str): The description to use in the JSON output. Defaults to the class docstring.
578
+ **kwargs: Extra key word arguments to be passed to `json.dumps()`
573
579
574
580
Returns:
575
581
str: The JSON string representation of the object.
576
582
"""
583
+ if "indent" not in kwargs :
584
+ kwargs ["indent" ] = 2
585
+
577
586
return json .dumps (
578
587
{
579
588
"type" : key_type or self .json_type ,
580
589
"description" : description or self .json_description ,
581
590
"cborHex" : self .to_cbor_hex (),
582
591
},
583
- indent = 2 ,
592
+ ** kwargs ,
584
593
)
585
594
586
595
@classmethod
@@ -613,6 +622,7 @@ def save(
613
622
path : str ,
614
623
key_type : Optional [str ] = None ,
615
624
description : Optional [str ] = None ,
625
+ ** kwargs ,
616
626
):
617
627
"""
618
628
Save the CBORSerializable object to a file in JSON format.
@@ -624,14 +634,15 @@ def save(
624
634
path (str): The file path to save the object to.
625
635
key_type (str, optional): The type to use in the JSON output. Defaults to the class name.
626
636
description (str, optional): The description to use in the JSON output. Defaults to the class docstring.
637
+ **kwargs: Extra key word arguments to be passed to `json.dumps()`
627
638
628
639
Raises:
629
640
IOError: If the file already exists and is not empty.
630
641
"""
631
642
if os .path .isfile (path ) and os .stat (path ).st_size > 0 :
632
643
raise IOError (f"File { path } already exists!" )
633
644
with open (path , "w" ) as f :
634
- f .write (self .to_json (key_type = key_type , description = description ))
645
+ f .write (self .to_json (key_type = key_type , description = description , ** kwargs ))
635
646
636
647
@classmethod
637
648
def load (cls , path : str ):
0 commit comments