I have a class with a child class. I'm able to serialize it into JSON using jsonpickle library. I'm trying to understand something better though. When I try to use Python's native json library, it says the object is not serializable.
I'm looking at this guide: https://www.programiz.com/python-programming/json
He's showing a class defined like this:
person_dict = {"name": "Bob",
"languages": ["English", "Fench"],
"married": True,
"age": 32
}
Which serializes OK.
print(json.dumps(person_dict))
# outputs "{"name": "Bob", "languages": ["English", "Fench"], "married": true, "age": 32}"
But I'm creating a class and instantiating an object from it.
class Purchase(object):
def __init__(self, receipt_id, order_id):
self.receipt_id = receipt_id
self.order_id = order_id
self.items = []
test = Purchase("123", "abc")
test.items.append("item1")
test.items.append("item2")
But json.dumps() doesn't work here.
print(json.dumps(test))
# outputs "TypeError: Object of type Purchase is not JSON serializable"
What fundamental am I missing here?