Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move exclude logic to improve performance #241

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 10 additions & 7 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,6 @@ def _encode_overrides(kvs, overrides, encode_json=False):
override_kvs = {}
for k, v in kvs.items():
if k in overrides:
exclude = overrides[k].exclude
# If the exclude predicate returns true, the key should be
# excluded from encoding, so skip the rest of the loop
if exclude and exclude(v):
continue
letter_case = overrides[k].letter_case
original_key = k
k = letter_case(k) if letter_case is not None else k
Expand Down Expand Up @@ -318,14 +313,22 @@ def _asdict(obj, encode_json=False):
source) to support arbitrary Collection and Mapping types.
"""
if _is_dataclass_instance(obj):
overrides = _user_overrides_or_exts(obj)
result = []
for field in fields(obj):
value = _asdict(getattr(obj, field.name), encode_json=encode_json)
value = getattr(obj, field.name)
exclude = overrides[field.name].exclude
# If the exclude predicate returns true, the key should be
# excluded from encoding, so skip the rest of the loop
if exclude and exclude(value):
continue

value = _asdict(value, encode_json=encode_json)
result.append((field.name, value))

result = _handle_undefined_parameters_safe(cls=obj, kvs=dict(result),
usage="to")
return _encode_overrides(dict(result), _user_overrides_or_exts(obj),
return _encode_overrides(dict(result), overrides,
encode_json=encode_json)
elif isinstance(obj, Mapping):
return dict((_asdict(k, encode_json=encode_json),
Expand Down