Edit on GitHub

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    skip_none: 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    * `skip_none`: When set to True, any field in the class with a None value is excluded from the
53    serialized output. Defaults to True.
54
55    If you want to use the other toml package, you can subclass `TomlSerializer` and implement
56    your own logic.
57    """
58    return se.serialize(
59        to_dict(
60            obj,
61            c=cls,
62            reuse_instances=reuse_instances,
63            convert_sets=convert_sets,
64            skip_none=skip_none,
65        ),
66        **opts,
67    )
68
69
70@overload
71def from_toml(
72    c: type[T], s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any
73) -> T: ...
74
75
76# For Union, Optional etc.
77@overload
78def from_toml(
79    c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any
80) -> Any: ...
81
82
83def from_toml(c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any) -> Any:
84    """
85    Deserialize from TOML into the object.
86
87    `c` is a class obejct and `s` is TOML string. If you supply keyword arguments other than `de`,
88    they will be passed in `toml.loads` function.
89
90    If you want to use the other toml package, you can subclass `TomlDeserializer` and implement
91    your own logic.
92    """
93    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:
84def from_toml(c: Any, s: str, de: type[Deserializer[str]] = TomlDeserializer, **opts: Any) -> Any:
85    """
86    Deserialize from TOML into the object.
87
88    `c` is a class obejct and `s` is TOML string. If you supply keyword arguments other than `de`,
89    they will be passed in `toml.loads` function.
90
91    If you want to use the other toml package, you can subclass `TomlDeserializer` and implement
92    your own logic.
93    """
94    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, skip_none: 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    skip_none: bool = True,
45    **opts: Any,
46) -> str:
47    """
48    Serialize the object into TOML.
49
50    You can pass any serializable `obj`. If you supply keyword arguments other than `se`,
51    they will be passed in `toml_w.dumps` function.
52
53    * `skip_none`: When set to True, any field in the class with a None value is excluded from the
54    serialized output. Defaults to True.
55
56    If you want to use the other toml package, you can subclass `TomlSerializer` and implement
57    your own logic.
58    """
59    return se.serialize(
60        to_dict(
61            obj,
62            c=cls,
63            reuse_instances=reuse_instances,
64            convert_sets=convert_sets,
65            skip_none=skip_none,
66        ),
67        **opts,
68    )

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.

  • skip_none: When set to True, any field in the class with a None value is excluded from the serialized output. Defaults to True.

If you want to use the other toml package, you can subclass TomlSerializer and implement your own logic.