serde.toml
Serialize and Deserialize in TOML format. This module depends on tomli (for python<=3.10) and tomli-w packages.
1""" 2Serialize and Deserialize in TOML format. This module depends on 3[tomli](https://github.com/hukkin/tomli) (for python<=3.10) and 4[tomli-w](https://github.com/hukkin/tomli-w) packages. 5""" 6 7import sys 8from typing import overload, Optional, Any 9 10import tomli_w 11 12from .compat import T 13from .de import Deserializer, from_dict 14from .se import Serializer, to_dict 15 16__all__ = ["from_toml", "to_toml"] 17 18 19if sys.version_info[:2] >= (3, 11): 20 import tomllib 21else: 22 import tomli as tomllib 23 24 25class TomlSerializer(Serializer[str]): 26 @classmethod 27 def serialize(cls, obj: Any, **opts: Any) -> str: 28 return tomli_w.dumps(obj, **opts) 29 30 31class TomlDeserializer(Deserializer[str]): 32 @classmethod 33 def deserialize(cls, data: str, **opts: Any) -> Any: 34 return tomllib.loads(data, **opts) 35 36 37def to_toml( 38 obj: Any, 39 cls: Optional[Any] = None, 40 se: type[Serializer[str]] = TomlSerializer, 41 reuse_instances: bool = False, 42 convert_sets: bool = True, 43 **opts: Any, 44) -> str: 45 """ 46 Serialize the object into TOML. 47 48 You can pass any serializable `obj`. If you supply keyword arguments other than `se`, 49 they will be passed in `toml_w.dumps` function. 50 51 If you want to use the other toml package, you can subclass `TomlSerializer` and implement 52 your own logic. 53 """ 54 return se.serialize( 55 to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts 56 ) 57 58 59@overload 60def from_toml( 61 c: type[T], s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any 62) -> T: ... 63 64 65# For Union, Optional etc. 66@overload 67def from_toml( 68 c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any 69) -> Any: ... 70 71 72def from_toml(c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any) -> Any: 73 """ 74 Deserialize from TOML into the object. 75 76 `c` is a class obejct and `s` is TOML string. If you supply keyword arguments other than `de`, 77 they will be passed in `toml.loads` function. 78 79 If you want to use the other toml package, you can subclass `TomlDeserializer` and implement 80 your own logic. 81 """ 82 return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
def
from_toml( c: Any, s: str, de: type[serde.de.Deserializer[str]] = <class 'serde.toml.TomlDeserializer'>, **opts: Any) -> Any:
73def from_toml(c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any) -> Any: 74 """ 75 Deserialize from TOML into the object. 76 77 `c` is a class obejct and `s` is TOML string. If you supply keyword arguments other than `de`, 78 they will be passed in `toml.loads` function. 79 80 If you want to use the other toml package, you can subclass `TomlDeserializer` and implement 81 your own logic. 82 """ 83 return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
Deserialize from TOML into the object.
c
is a class obejct and s
is TOML string. If you supply keyword arguments other than de
,
they will be passed in toml.loads
function.
If you want to use the other toml package, you can subclass TomlDeserializer
and implement
your own logic.
def
to_toml( obj: Any, cls: Optional[Any] = None, se: type[serde.se.Serializer[str]] = <class 'serde.toml.TomlSerializer'>, reuse_instances: bool = False, convert_sets: bool = True, **opts: Any) -> str:
38def to_toml( 39 obj: Any, 40 cls: Optional[Any] = None, 41 se: type[Serializer[str]] = TomlSerializer, 42 reuse_instances: bool = False, 43 convert_sets: bool = True, 44 **opts: Any, 45) -> str: 46 """ 47 Serialize the object into TOML. 48 49 You can pass any serializable `obj`. If you supply keyword arguments other than `se`, 50 they will be passed in `toml_w.dumps` function. 51 52 If you want to use the other toml package, you can subclass `TomlSerializer` and implement 53 your own logic. 54 """ 55 return se.serialize( 56 to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts 57 )
Serialize the object into TOML.
You can pass any serializable obj
. If you supply keyword arguments other than se
,
they will be passed in toml_w.dumps
function.
If you want to use the other toml package, you can subclass TomlSerializer
and implement
your own logic.