Using asdict instead of __dict__

This commit is contained in:
Brandon Rozek 2024-01-07 21:24:51 -05:00
parent d6d42ebe81
commit bfb50b2baf
No known key found for this signature in database
GPG key ID: 26E457DA82C9F480

View file

@ -13,7 +13,7 @@ both server and client run on the same
machine. machine.
""" """
from contextlib import contextmanager from contextlib import contextmanager
from dataclasses import dataclass from dataclasses import dataclass, asdict
from pathlib import Path from pathlib import Path
from threading import Thread from threading import Thread
from typing import Union from typing import Union
@ -235,12 +235,12 @@ def login(connection):
# Messages # Messages
## ##
class MessageEncoder(json.JSONEncoder): class DataclassEncoder(json.JSONEncoder):
def default(self, o): def default(self, o):
return o.__dict__ return asdict(o)
def send_message(connection, message): def send_message(connection, message):
contents = json.dumps(message, cls=MessageEncoder).encode() contents = json.dumps(message, cls=DataclassEncoder).encode()
connection.sendall(contents) connection.sendall(contents)
def receive_message(connection, cls=None): def receive_message(connection, cls=None):