@@ -163,20 +163,28 @@ def get_type_field(value):
163
163
)
164
164
)
165
165
166
- def convertwith (value , method ):
166
+ def convertwith (value , method , pass_identifier = False ):
167
167
if hasattr (value , method ) and isinstance (
168
168
getattr (value , method ), types .MethodType
169
169
):
170
- converted = getattr (value , method )() # type: ignore
170
+ if pass_identifier :
171
+ converted = getattr (value , method )(
172
+ python_class_identifier = python_class_identifier
173
+ ) # type: ignore
174
+ else :
175
+ converted = getattr (value , method )() # type: ignore
171
176
if isinstance (converted , dict ):
172
177
if add_type_field and (python_class_identifier not in converted .keys ()):
173
178
converted [python_class_identifier ] = get_type_field (value )
174
179
return adjust_dict ({k : v for k , v in converted .items ()})
175
180
else :
176
181
return adjust_dict (converted )
177
182
183
+ converted = convertwith (value , "to_dict" , pass_identifier = True )
184
+ if converted is not None :
185
+ return converted
178
186
for method in ("to_dict" , "asdict" , "dict" ):
179
- converted = convertwith (value , method )
187
+ converted = convertwith (value , method , pass_identifier = False )
180
188
if converted is not None :
181
189
return converted
182
190
if isinstance (value , str ):
@@ -368,8 +376,15 @@ def good_field(k: str):
368
376
if good_field (k )
369
377
}
370
378
# deserialize needs to do a shallow conversion from a dict as deep conversion is taken care of already.
379
+ #
371
380
if hasattr (python_class , "from_dict" ):
372
- return python_class .from_dict ({k : v for k , v in d .items ()}) # type: ignore
381
+ try :
382
+ return python_class .from_dict (
383
+ {k : v for k , v in d .items ()},
384
+ python_class_identifier = python_class_identifier ,
385
+ ) # type: ignore
386
+ except Exception :
387
+ return python_class .from_dict ({k : v for k , v in d .items ()}) # type: ignore
373
388
if deep :
374
389
d = {
375
390
k : deserialize (
0 commit comments