Skip to content

Commit

Permalink
python_class_identifier in to_dict during serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
yasserfarouk committed Dec 25, 2024
1 parent ca20bbc commit 2db2382
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/negmas/serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,28 @@ def get_type_field(value):
)
)

def convertwith(value, method):
def convertwith(value, method, pass_identifier=False):
if hasattr(value, method) and isinstance(
getattr(value, method), types.MethodType
):
converted = getattr(value, method)() # type: ignore
if pass_identifier:
converted = getattr(value, method)(
python_class_identifier=python_class_identifier
) # type: ignore
else:
converted = getattr(value, method)() # type: ignore
if isinstance(converted, dict):
if add_type_field and (python_class_identifier not in converted.keys()):
converted[python_class_identifier] = get_type_field(value)
return adjust_dict({k: v for k, v in converted.items()})
else:
return adjust_dict(converted)

converted = convertwith(value, "to_dict", pass_identifier=True)
if converted is not None:
return converted
for method in ("to_dict", "asdict", "dict"):
converted = convertwith(value, method)
converted = convertwith(value, method, pass_identifier=False)
if converted is not None:
return converted
if isinstance(value, str):
Expand Down Expand Up @@ -368,8 +376,15 @@ def good_field(k: str):
if good_field(k)
}
# deserialize needs to do a shallow conversion from a dict as deep conversion is taken care of already.
#
if hasattr(python_class, "from_dict"):
return python_class.from_dict({k: v for k, v in d.items()}) # type: ignore
try:
return python_class.from_dict(
{k: v for k, v in d.items()},
python_class_identifier=python_class_identifier,
) # type: ignore
except Exception:
return python_class.from_dict({k: v for k, v in d.items()}) # type: ignore
if deep:
d = {
k: deserialize(
Expand Down

0 comments on commit 2db2382

Please sign in to comment.