Getting TypeError when trying to serialize a custom class using Marshmallow in Python 3.9
Could someone explain Hey everyone, I'm running into an issue that's driving me crazy. After trying multiple solutions online, I still can't figure this out. I'm working with a `TypeError` when attempting to serialize a custom class using Marshmallow in Python 3.9. I have a simple model where I need to serialize an object that contains a list of nested objects. My model looks like this: ```python from marshmallow import Schema, fields, post_load class NestedItem: def __init__(self, name): self.name = name class MyItem: def __init__(self, title, nested_items): self.title = title self.nested_items = nested_items class NestedItemSchema(Schema): name = fields.Str() class MyItemSchema(Schema): title = fields.Str() nested_items = fields.List(fields.Nested(NestedItemSchema)) @post_load def make_my_item(self, data, **kwargs): return MyItem(**data) ``` When I try to serialize an instance of `MyItem` like this: ```python nested_items = [NestedItem('Item 1'), NestedItem('Item 2')] item = MyItem('My Title', nested_items) item_schema = MyItemSchema() result = item_schema.dump(item) print(result) ``` I get the following behavior: ``` TypeError: Object of type NestedItem is not JSON serializable ``` I've ensured that my schema is correctly defined, but it seems that Marshmallow doesn't know how to handle the `NestedItem` instances. I tried adding a custom field to handle serialization, but it didn't resolve the scenario. Could someone guide to understand how to properly serialize instances of custom classes with Marshmallow, or is there an additional step I might be missing? My development environment is Linux. What am I doing wrong? I'm coming from a different tech stack and learning Python 3.9. Cheers for any assistance! I'm working with Python 3.9 in a Docker container on Debian. Any help would be greatly appreciated!