Skip to content

Instantly share code, notes, and snippets.

@gonX
Created July 3, 2019 18:25
Show Gist options
  • Save gonX/b07b0f2270b7e6b9c98b9e1a1cd20a49 to your computer and use it in GitHub Desktop.
Save gonX/b07b0f2270b7e6b9c98b9e1a1cd20a49 to your computer and use it in GitHub Desktop.
Convert Python Dataclass to and from JSON
#!/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