Closed lmmx closed 1 year ago
On closer inspection, you cannot inherit raise_on_unknown_json_key = True
from a base class (which is probably why I chose to repeatedly call LoadMeta
), you don't get the raising behaviour. I'll just add the Meta
method instead to all classes.
from __future__ import annotations
from dataclasses import dataclass, field
from datetime import date
from dataclass_wizard import JSONWizard
# class BaseDeserialiser(JSONWizard):
# class Meta(JSONWizard.Meta):
# raise_on_unknown_json_key = True
#
#
@dataclass
class Lesson(JSONWizard):
Def: str
If: date
class Meta(JSONWizard.Meta):
raise_on_unknown_json_key = True
key_transform_with_load = "PASCAL"
@dataclass
class ClassInfo(JSONWizard):
Class: str
For: str
And: str
From: date
Values: list[Lesson] = field(default_factory=list)
class Meta(JSONWizard.Meta):
raise_on_unknown_json_key = True
key_transform_with_load = "PASCAL"
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)
Implemented in 7d24b49
Rather than repeatedly call LoadMeta, use inheritance
We currently do
whereas in the proof of concept I did
then defined dataclasses like
Either:
Base(JSONWizard)
and defines theMeta
methodMixin
and defines the Meta Method, then use multiple inheritanceThe proof of concept shows that option 1 is viable, so that should be used.