Skip to content

Commit

Permalink
lidatong#305: Split dataclass parser and field value parser
Browse files Browse the repository at this point in the history
  • Loading branch information
USSX-Hares committed Jul 20, 2021
1 parent ec7a641 commit db418c0
Showing 1 changed file with 28 additions and 31 deletions.
59 changes: 28 additions & 31 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,41 +172,38 @@ def _decode_dataclass(cls, kvs, infer_missing):
init_kwargs[field.name] = field_value
continue

while True:
if not _is_new_type(field_type):
break
init_kwargs[field.name] = _decode_field(field_type, field_value, infer_missing=infer_missing, override=overrides.get(field.name, None))

field_type = field_type.__supertype__
return cls(**init_kwargs)

if (field.name in overrides
and overrides[field.name].decoder is not None):
# FIXME hack
if field_type is type(field_value):
init_kwargs[field.name] = field_value
else:
init_kwargs[field.name] = overrides[field.name].decoder(
field_value)
elif is_dataclass(field_type):
# FIXME this is a band-aid to deal with the value already being
# serialized when handling nested marshmallow schema
# proper fix is to investigate the marshmallow schema generation
# code
if is_dataclass(field_value):
value = field_value
else:
value = _decode_dataclass(field_type, field_value,
infer_missing)
init_kwargs[field.name] = value
elif _is_supported_generic(field_type) and field_type != str:
init_kwargs[field.name] = _decode_generic(field_type,
field_value,
infer_missing)
else:
init_kwargs[field.name] = _support_extended_types(field_type,
field_value)

return cls(**init_kwargs)
def _decode_field(field_type, field_value, *, infer_missing: bool, override = None):
while True:
if not _is_new_type(field_type):
break

field_type = field_type.__supertype__

if (override is not None and override.decoder is not None):
# FIXME hack
if field_type is type(field_value):
return field_value
else:
return override.decoder(field_value)
elif is_dataclass(field_type):
# FIXME this is a band-aid to deal with the value already being
# serialized when handling nested marshmallow schema
# proper fix is to investigate the marshmallow schema generation
# code
if is_dataclass(field_value):
value = field_value
else:
value = _decode_dataclass(field_type, field_value, infer_missing=infer_missing)
return value
elif _is_supported_generic(field_type) and field_type != str:
return _decode_generic(field_type, field_value, infer_missing=infer_missing)
else:
return _support_extended_types(field_type, field_value)

def _support_extended_types(field_type, field_value):
if _issubclass_safe(field_type, datetime):
Expand Down

0 comments on commit db418c0

Please sign in to comment.