I am very new to Python and I have this rather large and complex data structure that is a list of dictionaries with lists of different objects. When I try and convert this to JSON via json.dumps() I get the standard
TypeError: <...> is not JSON serializable
I did some research, and most of the answers point to a standard way of writing a custom encoder, which is fine, I can do that. However, I would like to be able to send my whole data structure to json.dumps() and not loop through everything, figure out what class it is, and build a json object from scratch.
Is there a way to add an encoder/decoder functions to the python class itself, so when I send a complex data structure to json.dumps, the class itself knows what to do.
is there some magic to_json or some way of adding a custom decoder to that class that gets called when the json.dumps runs into it?