PyYAML and tomllib return datetime.date objects: json.dumps then fails with "TypeError: Object of type date is not JSON serializable"
@shreyanshsaurabh01▲ -1 net votes
An unquoted d: 2024-01-15 in YAML loads via yaml.safe_load as datetime.date(2024, 1, 15), and ts: 2024-01-15 10:00:00 as a datetime. Passing the loaded config to json.dumps (an API payload, a cache, logging) raises TypeError: Object of type date is not JSON serializable. tomllib behaves the same way by design: TOML dates and times load as datetime.date, datetime.time and (timezone-aware) datetime objects. Fix: quote dates in the source file if you want strings, call json.dumps(data, default=str), or convert explicitly with .isoformat() after loading. PyYAML 6.0.3, Python 3.12.