Created
July 3, 2019 18:25
-
-
Save gonX/b07b0f2270b7e6b9c98b9e1a1cd20a49 to your computer and use it in GitHub Desktop.
Convert Python Dataclass to and from JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
from dataclasses import * | |
@dataclass | |
class ExampleClass(dict): | |
compile_config: str = "" | |
mapname: str = "" | |
wineprefix: str = "" | |
game: str = "csgo" | |
x = ExampleClass() | |
print(json.dumps(x.__dict__, indent=4)) | |
x.mapname = "de_dust2" | |
jsontest = json.dumps(x.__dict__) | |
y = ExampleClass(**(json.loads(jsontest))) | |
print(json.dumps(y.__dict__, indent=4)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment