@@ -561,29 +561,35 @@ def json_description(self) -> str:
561561 """
562562 return self .__class__ .__doc__ or "Generated with PyCardano"
563563
564- def to_json (self , ** kwargs ) -> str :
564+ def to_json (
565+ self ,
566+ key_type : Optional [str ] = None ,
567+ description : Optional [str ] = None ,
568+ ** kwargs ,
569+ ) -> str :
565570 """
566571 Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex.
567572
568573 This method returns a JSON representation of the object, including its type, description, and CBOR hex encoding.
569574
570575 Args:
571- **kwargs: Additional keyword arguments that can include:
572- - key_type (str): The type to use in the JSON output. Defaults to the class name .
573- - description (str): The description to use in the JSON output. Defaults to the class docstring.
576+ key_type (str): The type to use in the JSON output. Defaults to the class name.
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()`
574579
575580 Returns:
576581 str: The JSON string representation of the object.
577582 """
578- key_type = kwargs .pop ("key_type" , self .json_type )
579- description = kwargs .pop ("description" , self .json_description )
583+ if "indent" not in kwargs :
584+ kwargs ["indent" ] = 2
585+
580586 return json .dumps (
581587 {
582- "type" : key_type ,
583- "description" : description ,
588+ "type" : key_type or self . json_type ,
589+ "description" : description or self . json_description ,
584590 "cborHex" : self .to_cbor_hex (),
585591 },
586- indent = 2 ,
592+ ** kwargs ,
587593 )
588594
589595 @classmethod
@@ -616,6 +622,7 @@ def save(
616622 path : str ,
617623 key_type : Optional [str ] = None ,
618624 description : Optional [str ] = None ,
625+ ** kwargs ,
619626 ):
620627 """
621628 Save the CBORSerializable object to a file in JSON format.
@@ -625,16 +632,17 @@ def save(
625632
626633 Args:
627634 path (str): The file path to save the object to.
628- key_type (str, optional): The type to use in the JSON output.
629- description (str, optional): The description to use in the JSON output.
635+ key_type (str, optional): The type to use in the JSON output. Defaults to the class name.
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()`
630638
631639 Raises:
632640 IOError: If the file already exists and is not empty.
633641 """
634642 if os .path .isfile (path ) and os .stat (path ).st_size > 0 :
635643 raise IOError (f"File { path } already exists!" )
636644 with open (path , "w" ) as f :
637- f .write (self .to_json (key_type = key_type , description = description ))
645+ f .write (self .to_json (key_type = key_type , description = description , ** kwargs ))
638646
639647 @classmethod
640648 def load (cls , path : str ):
0 commit comments