Created
May 25, 2023 07:53
-
-
Save lmmx/e55ea8fa962217f923d7aa5c69019a0d to your computer and use it in GitHub Desktop.
Ingesting a DTO with reserved word keys using dataclasses (JSONWizard helper class from the dataclass-wizard package). One way is to name all the classes manually, but better is to set the letter case to 'PASCAL' which will 'upper camel case' them. The only cases this won't work for are if "True", "False", or "None" are keys in the DTO.
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
from __future__ import annotations | |
from dataclasses import dataclass, field | |
from datetime import date | |
from dataclass_wizard import JSONWizard, json_field | |
@dataclass | |
class Lesson(JSONWizard): | |
_def: str = json_field('def', all=True) | |
_if: date = json_field('if', all=True) | |
@dataclass | |
class ClassInfo(JSONWizard): | |
_class: str = json_field('class', all=True) | |
_for: str = json_field('for', all=True) | |
_and: str = json_field('and', all=True) | |
_from: date = json_field('from', all=True) | |
values: list[Lesson] = field(default_factory=list) | |
json_string = """ | |
{ | |
"class": "Math", | |
"for": "Beginners", | |
"and": "Intermediate", | |
"from": "2023-05-25", | |
"values": [ | |
{ | |
"def": "Geometry", | |
"if": "2023-06-01" | |
}, | |
{ | |
"def": "Algebra", | |
"if": "2023-07-01" | |
} | |
] | |
} | |
""" | |
class_info = ClassInfo.from_json(json_string) |
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
from __future__ import annotations | |
from dataclasses import dataclass, field | |
from datetime import date | |
from dataclass_wizard import JSONWizard | |
@dataclass | |
class Lesson(JSONWizard): | |
class Meta(JSONWizard.Meta): | |
key_transform_with_load = "PASCAL" | |
Def: str | |
If: date | |
@dataclass | |
class ClassInfo(JSONWizard): | |
class Meta(JSONWizard.Meta): | |
key_transform_with_load = "PASCAL" | |
Class: str | |
For: str | |
And: str | |
From: date | |
Values: list[Lesson] = field(default_factory=list) | |
json_string = """ | |
{ | |
"class": "Math", | |
"for": "Beginners", | |
"and": "Intermediate", | |
"from": "2023-05-25", | |
"values": [ | |
{"def": "Geometry", "if": "2023-06-01"}, | |
{"def": "Algebra", "if": "2023-07-01"} | |
] | |
} | |
""" | |
class_info = ClassInfo.from_json(json_string) |
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
{ | |
"class": "Math", | |
"for": "Beginners", | |
"and": "Intermediate", | |
"from": "2023-05-25", | |
"values": [ | |
{ | |
"def": "Geometry", | |
"if": "2023-06-01" | |
}, | |
{ | |
"def": "Algebra", | |
"if": "2023-07-01" | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment