Python arbitrary objects Serialization and Deserialization
There's a library for this dataclasses-json. Whilst it's a pretty poor library the internal code is bad, there's few tests, the documentation is quite small and the design is starting to suffer from these poor decisions. It works, and for your code it is good enough.
You should be able to see that all serialization and deserialization is performed automatically. This is good as then you can focus on using the objects rather than converting to and from them.
from __future__ import annotations

from dataclasses import dataclass

from dataclasses_json import dataclass_json


@dataclass_json
@dataclass
class Foo:
    x: int
    y: int
    bar: Bar


@dataclass_json
@dataclass
class Bar:
    z: int


raw = '{&quot;x&quot;: 100, &quot;y&quot;: 500, &quot;bar&quot;: {&quot;z&quot;: 900}}'
obj = Foo.from_json(raw)
assert obj == Foo(100, 500, Bar(900))
assert obj.to_json() == raw