Python·100 questions

How to serialize dataclass to dict/JSON?

Answer
dataclasses.asdict(obj) → dict.
Then json.dumps(dict).
Convert datetime to string.
asdict works recursively for nested objects.
Use pydantic or explicit code for format control.
Was this answer helpful?

More questions in this topic

Related questions from other topics