tweaks for server using serialization
This commit is contained in:
@@ -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)."""
|
||||
|
||||
Reference in New Issue
Block a user