konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.76k stars 106 forks source link

how to to_dict? #56

Closed daiooo closed 5 years ago

daiooo commented 5 years ago

tks

konradhalas commented 5 years ago

You can use dataclasses.asdict.

emmeowzing commented 6 months ago

For anyone else who comes here looking for the same thing, here's an example:

>>> import dacite
>>> from dataclasses import dataclass, asdict
>>> @dataclass
... class A:
...   a: int
...
>>> @dataclass
... class B:
...   a: A
...   b: int
>>> asdict(dacite.from_dict(B, {'a': { 'a': 1 }, 'b': 2}))
{'a': {'a': 1}, 'b': 2}