Overview¶
Requirements¶
The dataclass-mage library officially supports Python 3.9+
There are no core requirements outside of the Python standard library.
Advantages¶
Minimal setup required. In most cases, all you need is a dataclass that sub-classes from
JSONWizard.Speed. It is up to 25 times faster than libraries such as dataclasses-json that use
marshmallow, and about 60 x faster than libraries such as jsons which don’t seem to handle dataclasses as well as you’d expect.Adds the ability to use field properties (with default values) in dataclasses.
Automatic key transform to/from JSON (ex. camel to snake). Custom key mappings also supported.
Automatic type conversion when loading from JSON or a
dictobject. For instance, strings are converted to boolean if a type annotation isList[bool].Built-in support for standard Python collections, as well as most Generics from the
typingmodule. Other commonly used types such as Enums, defaultdict, and date and time objects such asdatetimeare also natively supported.Latest Python features such as parameterized standard collections can be used.
Ability to construct ad-hoc dataclass schemas using JSON input (either as a file or string) using the included wiz-cli utility.
Supported Types¶
Tip
See the below section on Special Cases for additional info on the JSON load/dump process for special Python types.
- Strings
strbytesbytearray
- Numerics
intfloatDecimal
Booleans (
bool)- Sequences (and their equivalents in the
typingmodule) listdequetupleNamedTuple
- Sequences (and their equivalents in the
- Sets (and their equivalents in the
typingmodule) setfrozenset
- Sets (and their equivalents in the
- Mappings (and their equivalents in the
typingmodule) dictdefaultdictTypedDictOrderedDict
- Mappings (and their equivalents in the
EnumsubclassesUUID- date and time objects
datetimedatetimetimedelta
- Special typing primitives from the
typingmodule AnyUnion- Also supports using dataclasses.Optional
- Special typing primitives from the
- Recently introduced Generic types
AnnotatedLiteral
Special Cases¶
Note
With most annotated Python types, it is clear and unambiguous how they are to be loaded from JSON, or dumped when they are serialized back to JSON.
However, here a few special cases that are worth going over.
bool- JSON values that appear as strings or integers will be de-serialized to aboolusing a case-insensitive search that matches against the following “truthy” values:TRUE, T, YES, Y, 1
Enum- JSON values (ideally strings) are de-serialized toEnumsubclasses via thevalueattribute, and are serialized back to JSON using the samevalueattribute.UUIDtypes are de-serialized from JSON strings using the constructor method – i.e.UUID(string), and by default are serialized back to JSON using thehexattribute – i.e.my_uuid.hex.Decimaltypes are de-serialized using theDecimal(str(o))syntax – or via an annotated subclass of Decimal – and are serialized via the builtinstr()function.NamedTuplesub-types are de-serialized from alist,tuple, or any iterable type into the annotated sub-type. They are serialized back as the the annotatedNamedTuplesub-type; this is mainly because named tuples are essentially just tuples, so they are inherently JSON serializable to begin with.For
date,time, anddatetimetypes, string values are de-serialized using the builtinfromisoformat()method; fordatetimeandtimetypes, a suffix of “Z” appearing in the string is first replaced with “+00:00”, which represents UTC time. JSON values fordatetimeanddateannotated types appearing as numbers will be de-serialized using the builtinfromtimestamp()method.All these types are serialized back to JSON using the builtin
isoformat()method. Fordatetimeandtimetypes, there is one noteworthy addition: the suffix “+00:00” is replaced with “Z”, which is a common abbreviation for UTC time.For
timedeltatypes, the values to de-serialize can either be strings or numbers, so we check the type explicitly. If the value is a string, we first ensure it’s in a numeric form like ‘1.23’, and if so convert it to a float value in seconds; otherwise, we convert values like ‘01:45’ or ‘3hr12m56s’ via the pytimeparse module, which is also available as an extra viapip install dataclass-mage[timedelta]. Lastly, any numeric values are assumed to be in seconds and are used as is.All
timedeltavalues are serialized back to JSON using the builtinstr()method, so for exampletimedelta(seconds=3)will be serialized as “0:00:03”.set,frozenset, anddequetypes will be de-serialized using their annotated base types, and serialized aslist’s.Commonly used
dictsub-types (such asdefaultdict) will be de-serialized from JSON objects using the annotated base type, and serialized back as plaindictobjects.