Skip to content
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
2 changes: 2 additions & 0 deletions pycardano/address.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ def save(
path: str,
key_type: Optional[str] = None,
description: Optional[str] = None,
**kwargs,
):
"""
Save the Address object to a file.
Expand All @@ -427,6 +428,7 @@ def save(
path (str): The file path to save the object to.
key_type (str, optional): Not used in this context, but can be included for consistency.
description (str, optional): Not used in this context, but can be included for consistency.
**kwargs: Additional keyword arguments (not used here).
Raises:
IOError: If the file already exists and is not empty.
Expand Down
4 changes: 2 additions & 2 deletions pycardano/key.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def to_primitive(self) -> bytes:
def from_primitive(cls: Type["Key"], value: bytes) -> Key:
return cls(value)

def to_json(self, **kwargs) -> str:
def to_json(self, **kwargs) -> str: # type: ignore
"""Serialize the key to JSON.
The json output has three fields: "type", "description", and "cborHex".
Expand All @@ -90,7 +90,7 @@ def to_json(self, **kwargs) -> str:
)

@classmethod
def from_json(cls: Type[Key], data: str, validate_type=False) -> Key:
def from_json(cls: Type[Key], data: str, validate_type=False) -> Key: # type: ignore
"""Restore a key from a JSON string.
Args:
Expand Down
4 changes: 2 additions & 2 deletions pycardano/plutus.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,7 +652,7 @@ def _dfs(obj):

return _dfs(self)

def to_json(self, **kwargs) -> str:
def to_json(self, **kwargs) -> str: # type: ignore
"""Convert to a json string
Args:
Expand Down Expand Up @@ -847,7 +847,7 @@ def _dfs(obj):

return _dfs(RawPlutusData.to_primitive(self))

def to_json(self, **kwargs) -> str:
def to_json(self, **kwargs) -> str: # type: ignore
"""Convert to a json string
Args:
Expand Down
32 changes: 20 additions & 12 deletions pycardano/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,29 +561,35 @@ def json_description(self) -> str:
"""
return self.__class__.__doc__ or "Generated with PyCardano"

def to_json(self, **kwargs) -> str:
def to_json(
self,
key_type: Optional[str] = None,
description: Optional[str] = None,
**kwargs,
) -> str:
"""
Convert the CBORSerializable object to a JSON string containing type, description, and CBOR hex.

This method returns a JSON representation of the object, including its type, description, and CBOR hex encoding.

Args:
**kwargs: Additional keyword arguments that can include:
- key_type (str): The type to use in the JSON output. Defaults to the class name.
- description (str): The description to use in the JSON output. Defaults to the class docstring.
key_type (str): The type to use in the JSON output. Defaults to the class name.
description (str): The description to use in the JSON output. Defaults to the class docstring.
**kwargs: Extra key word arguments to be passed to `json.dumps()`

Returns:
str: The JSON string representation of the object.
"""
key_type = kwargs.pop("key_type", self.json_type)
description = kwargs.pop("description", self.json_description)
if "indent" not in kwargs:
kwargs["indent"] = 2

return json.dumps(
{
"type": key_type,
"description": description,
"type": key_type or self.json_type,
"description": description or self.json_description,
"cborHex": self.to_cbor_hex(),
},
indent=2,
**kwargs,
)

@classmethod
Expand Down Expand Up @@ -616,6 +622,7 @@ def save(
path: str,
key_type: Optional[str] = None,
description: Optional[str] = None,
**kwargs,
):
"""
Save the CBORSerializable object to a file in JSON format.
Expand All @@ -625,16 +632,17 @@ def save(

Args:
path (str): The file path to save the object to.
key_type (str, optional): The type to use in the JSON output.
description (str, optional): The description to use in the JSON output.
key_type (str, optional): The type to use in the JSON output. Defaults to the class name.
description (str, optional): The description to use in the JSON output. Defaults to the class docstring.
**kwargs: Extra key word arguments to be passed to `json.dumps()`

Raises:
IOError: If the file already exists and is not empty.
"""
if os.path.isfile(path) and os.stat(path).st_size > 0:
raise IOError(f"File {path} already exists!")
with open(path, "w") as f:
f.write(self.to_json(key_type=key_type, description=description))
f.write(self.to_json(key_type=key_type, description=description, **kwargs))

@classmethod
def load(cls, path: str):
Expand Down
2 changes: 1 addition & 1 deletion pycardano/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ class Transaction(ArrayCBORSerializable):
def json_type(self) -> str:
return (
"Unwitnessed Tx ConwayEra"
if self.transaction_witness_set.is_empty()
if self.transaction_witness_set.vkey_witnesses is None
else "Signed Tx ConwayEra"
)

Expand Down
Loading