"""Preconfigured converters for the stdlib json."""frombase64importb85decode,b85encodefromdatetimeimportdatetimefromjsonimportdumps,loadsfromtypingimportAny,Type,TypeVar,Unionfromcattrs._compatimportAbstractSet,Counterfrom..convertersimportBaseConverter,ConverterT=TypeVar("T")
[docs]defconfigure_converter(converter:BaseConverter):""" Configure the converter for use with the stdlib json module. * bytes are serialized as base64 strings * datetimes are serialized as ISO 8601 * counters are serialized as dicts * sets are serialized as lists """converter.register_unstructure_hook(bytes,lambdav:(b85encode(v)ifvelseb"").decode("utf8"))converter.register_structure_hook(bytes,lambdav,_:b85decode(v))converter.register_unstructure_hook(datetime,lambdav:v.isoformat())converter.register_structure_hook(datetime,lambdav,_:datetime.fromisoformat(v))