I'm trying to convert object into JSON. Converting from JSON to object was simple. When I try to use the dumps method, I'm getting the error: TypeError: Object of type datetime is not JSON serializable. Is this possible to convert such class?
import json
import datetime
from types import SimpleNamespace
class Outcome:
def __init__(self, name='', date=datetime.datetime.now(), price=0.0):
self.name = name
self.date = datetime.datetime.strptime(date, '%Y-%m-%d')
self.price = float(price)
# convert from json to outcome
txt = b'{"name": "milk", "date": "2020-01-02", "price": 1.99}'
txt = txt.decode("utf-8")
x = json.loads(txt, object_hook=lambda d: SimpleNamespace(**d))
o = Outcome(x.name, x.date, x.price)
print(o.name, o.date, o.price)
# convert outcome to json (how?)
o2 = json.dumps(o.__dict__)
print(o2.name, o2.date, o2.price)