konradhalas / dacite

Simple creation of data classes from dictionaries.
MIT License
1.72k stars 106 forks source link

add possibility use keywords from reserved names. #220

Open AlexSamarsky opened 1 year ago

AlexSamarsky commented 1 year ago

i want to load json with key name 'from', make dataclasse with field "from_" and load it if config.reserved_names is True i found that nessesary when worked with JIRA API it returns change history json

    data = {
                "field": "IssueParentAssociation",
                "fieldtype": "jira",
                "from": "88223",
                "fromString": "PRJ-1",
                "to": "88222",
                "toString": "PRJ-2"
              }

then make dataclass

@dataclass
class HistoryItemJson:
    field: str
    fieldtype: str
    fieldId: Optional[str]
    from_: Optional[str]
    fromString: Optional[str]
    to: Optional[str]
    toString: Optional[str]

and simply load it result: HistoryItemJson = from_dict(data_class=HistoryItemJson, data=data, config=Config(type_hooks={datetime: datetime.fromisoformat}, reserved_names=True))