tweaks for server using serialization

This commit is contained in:
2026-01-26 10:11:44 +01:00
parent 6e14cb65c3
commit 0504e90754
8 changed files with 90 additions and 43 deletions

View File

@@ -11,7 +11,7 @@ class Serializable:
instance.deserialize_dict(data)
return instance
def serialize(self, file) -> str:
def serialize(self, file=None) -> str:
"""Serializes the object and all nested Serializable objects to JSON."""
def serialize_value(value: Any) -> Any:
if isinstance(value, Serializable):
@@ -24,7 +24,9 @@ class Serializable:
return value
attrs = {k: serialize_value(v) for k, v in self.__dict__.items() if not k.startswith('_')}
return json.dumps(attrs, file, ensure_ascii=False, indent=2)
if file:
json.dump(attrs, file, ensure_ascii=False, indent=2)
return json.dumps(attrs, ensure_ascii=False, indent=2)
def serialize_dict(self) -> Dict[str, Any]:
"""Serializes the object to a dictionary (for nested serialization)."""