I want to make my class JSON serializeable and am trying to extend it with the JSONEncoder class. However, why does the default function take the o parameter when it is supposed to be a method of my class.
I want to serialize my class object, not a third object that is passed to the method?
Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).
def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
# Let the base class default method raise the TypeError
return JSONEncoder.default(self, o)
JSONEncoder... that is your fundamental misunderstanding.