How to properly use python json_dump_obj for custom object serialization? or Why is python json_dump_

16 Replies, 1217 Views

Dude, python json_dump_obj needs a little help with custom classes.

You can use the `dataclasses` module if you’re on Python 3.7+:
```python
from dataclasses import dataclass, asdict
import json

@dataclass
class YourClass:
attr1: str
attr2: int

json.dumps(asdict(your_obj))
```

Super clean and no extra work.

Messages In This Thread



Users browsing this thread: 1 Guest(s)