Skip to content

feat: Add option to convert frozen dataclasses to frozendicts #416

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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: 15 additions & 2 deletions dataclasses_json/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,21 @@ def from_dict(cls: Type[A],
infer_missing=False) -> A:
return _decode_dataclass(cls, kvs, infer_missing)

def to_dict(self, encode_json=False) -> Dict[str, Json]:
return _asdict(self, encode_json=encode_json)
def to_dict(self,
encode_json=False,
use_frozendict=False
) -> Dict[str, Json]:
"""
Converts a dataclass object into a dictionary.

:param use_frozendict: If set to True, frozen dataclasses will be
converted to frozendicts instead of normal dicts.

:returns: A dictionary representation of the dataclass object.
"""
return _asdict(self,
encode_json=encode_json,
use_frozendict=use_frozendict)

@classmethod
def schema(cls: Type[A],
Expand Down
12 changes: 9 additions & 3 deletions dataclasses_json/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from typing import (Any, Collection, Mapping, Union, get_type_hints,
Tuple, TypeVar)
from uuid import UUID
from frozendict import frozendict

from typing_inspect import is_union_type # type: ignore

Expand Down Expand Up @@ -340,7 +341,7 @@ def _decode_items(type_arg, xs, infer_missing):
return items


def _asdict(obj, encode_json=False):
def _asdict(obj, encode_json=False, use_frozendict=False):
"""
A re-implementation of `asdict` (based on the original in the `dataclasses`
source) to support arbitrary Collection and Mapping types.
Expand All @@ -360,8 +361,13 @@ def _asdict(obj, encode_json=False):

result = _handle_undefined_parameters_safe(cls=obj, kvs=dict(result),
usage="to")
return _encode_overrides(dict(result), _user_overrides_or_exts(obj),
encode_json=encode_json)
result = _encode_overrides(dict(result), _user_overrides_or_exts(obj),
encode_json=encode_json)

if use_frozendict and obj.__dataclass_params__.frozen:
result = frozendict(result)

return result
elif isinstance(obj, Mapping):
return dict((_asdict(k, encode_json=encode_json),
_asdict(v, encode_json=encode_json)) for k, v in
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
'dataclasses;python_version=="3.6"',
'marshmallow>=3.3.0,<4.0.0',
'marshmallow-enum>=1.5.1,<2.0.0',
'typing-inspect>=0.4.0'
'typing-inspect>=0.4.0',
'frozendict>=2.3.8'
],
python_requires='>=3.6',
extras_require={
Expand Down