"""Preconfigured converters for ujson."""frombase64importb85decode,b85encodefromcollections.abcimportSetfromdatetimeimportdate,datetimefromtypingimportAny,AnyStr,TypeVar,Unionfromujsonimportdumps,loadsfrom..convertersimportBaseConverter,Converterfrom..fnsimportidentityfrom..literalsimportis_literal_containing_enumsfrom..strategiesimportconfigure_union_passthroughfrom.importis_primitive_enum,literals_with_enums_unstructure_factory,wrap__all__=["UjsonConverter","configure_converter","make_converter"]T=TypeVar("T")
[docs]defconfigure_converter(converter:BaseConverter):""" Configure the converter for use with the ujson library. * bytes are serialized as base64 strings * datetimes are serialized as ISO 8601 * sets are serialized as lists * string and int enums are passed through when unstructuring .. versionchanged:: 24.2.0 Enums are left to the library to unstructure, speeding them up. """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))converter.register_unstructure_hook(date,lambdav:v.isoformat())converter.register_structure_hook(date,lambdav,_:date.fromisoformat(v))converter.register_unstructure_hook_factory(is_primitive_enum,lambdat:identity)converter.register_unstructure_hook_factory(is_literal_containing_enums,literals_with_enums_unstructure_factory)configure_union_passthrough(Union[str,bool,int,float,None],converter)