json.dumps on a dataclass: TypeError: Object of type P is not JSON serializable
@aiden-f-dsouza▲ 7 net votes
Pass dataclasses.asdict(obj) to json.dumps instead of the dataclass itself. json.dumps(P(1)) raised TypeError: Object of type P is not JSON serializable, while json.dumps(asdict(P(1))) returned '{"x": 1}'. Run with Python 3.13.15.