Edit on GitHub

serde.yaml

Serialize and Deserialize in YAML format. This module depends on pyyaml package.

 1"""
 2Serialize and Deserialize in YAML format. This module depends on
 3[pyyaml](https://pypi.org/project/PyYAML/) package.
 4"""
 5
 6from typing import overload, Optional, Any
 7
 8import yaml
 9
10from .compat import T
11from .de import Deserializer, from_dict
12from .se import Serializer, to_dict
13
14__all__ = ["from_yaml", "to_yaml"]
15
16
17class YamlSerializer(Serializer[str]):
18    @classmethod
19    def serialize(cls, obj: Any, **opts: Any) -> str:
20        return yaml.safe_dump(obj, **opts)  # type: ignore
21
22
23class YamlDeserializer(Deserializer[str]):
24    @classmethod
25    def deserialize(cls, data: str, **opts: Any) -> Any:
26        return yaml.safe_load(data, **opts)
27
28
29def to_yaml(
30    obj: Any,
31    cls: Optional[Any] = None,
32    se: type[Serializer[str]] = YamlSerializer,
33    reuse_instances: bool = False,
34    convert_sets: bool = True,
35    skip_none: bool = False,
36    **opts: Any,
37) -> str:
38    """
39    Serialize the object into YAML.
40
41    You can pass any serializable `obj`. If you supply keyword arguments other than `se`,
42    they will be passed in `yaml.safe_dump` function.
43
44    * `skip_none`: When set to True, any field in the class with a None value is excluded from the
45    serialized output. Defaults to False.
46
47    If you want to use the other yaml package, you can subclass `YamlSerializer` and implement
48    your own logic.
49    """
50    return se.serialize(
51        to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts
52    )
53
54
55@overload
56def from_yaml(
57    c: type[T], s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any
58) -> T: ...
59
60
61# For Union, Optional etc.
62@overload
63def from_yaml(
64    c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any
65) -> Any: ...
66
67
68def from_yaml(c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any) -> Any:
69    """
70    `c` is a class obejct and `s` is YAML string. If you supply keyword arguments other than `de`,
71    they will be passed in `yaml.safe_load` function.
72
73    If you want to use the other yaml package, you can subclass `YamlDeserializer` and implement
74    your own logic.
75    """
76    return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)
def from_yaml( c: Any, s: str, de: type[serde.de.Deserializer[str]] = <class 'serde.yaml.YamlDeserializer'>, **opts: Any) -> Any:
69def from_yaml(c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any) -> Any:
70    """
71    `c` is a class obejct and `s` is YAML string. If you supply keyword arguments other than `de`,
72    they will be passed in `yaml.safe_load` function.
73
74    If you want to use the other yaml package, you can subclass `YamlDeserializer` and implement
75    your own logic.
76    """
77    return from_dict(c, de.deserialize(s, **opts), reuse_instances=False)

c is a class obejct and s is YAML string. If you supply keyword arguments other than de, they will be passed in yaml.safe_load function.

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

def to_yaml( obj: Any, cls: Optional[Any] = None, se: type[serde.se.Serializer[str]] = <class 'serde.yaml.YamlSerializer'>, reuse_instances: bool = False, convert_sets: bool = True, skip_none: bool = False, **opts: Any) -> str:
30def to_yaml(
31    obj: Any,
32    cls: Optional[Any] = None,
33    se: type[Serializer[str]] = YamlSerializer,
34    reuse_instances: bool = False,
35    convert_sets: bool = True,
36    skip_none: bool = False,
37    **opts: Any,
38) -> str:
39    """
40    Serialize the object into YAML.
41
42    You can pass any serializable `obj`. If you supply keyword arguments other than `se`,
43    they will be passed in `yaml.safe_dump` function.
44
45    * `skip_none`: When set to True, any field in the class with a None value is excluded from the
46    serialized output. Defaults to False.
47
48    If you want to use the other yaml package, you can subclass `YamlSerializer` and implement
49    your own logic.
50    """
51    return se.serialize(
52        to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts
53    )

Serialize the object into YAML.

You can pass any serializable obj. If you supply keyword arguments other than se, they will be passed in yaml.safe_dump function.

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

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