[b]"How to properly use python json_dump_obj for custom object serialization?"[/b] or [b]"Why is python json_dump_

16 Replies, 906 Views

Yup, python json_dump_obj doesn’t know how to handle your class unless you tell it how.

Override `__json__` or use `json.JSONEncoder`. Here’s a quick example:
```python
class YourClass:
def __json__(self):
return {'attr1': self.attr1, 'attr2': self.attr2}
```

Or subclass `JSONEncoder` and pass it to `json.dumps()`.

Messages In This Thread



Users browsing this thread: 1 Guest(s)