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    **opts: Any,
36) -> str:
37    """
38    Serialize the object into YAML.
39
40    You can pass any serializable `obj`. If you supply keyword arguments other than `se`,
41    they will be passed in `yaml.safe_dump` function.
42
43    If you want to use the other yaml package, you can subclass `YamlSerializer` and implement
44    your own logic.
45    """
46    return se.serialize(
47        to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts
48    )
49
50
51@overload
52def from_yaml(
53    c: type[T], s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any
54) -> T: ...
55
56
57# For Union, Optional etc.
58@overload
59def from_yaml(
60    c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any
61) -> Any: ...
62
63
64def from_yaml(c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any) -> Any:
65    """
66    `c` is a class obejct and `s` is YAML string. If you supply keyword arguments other than `de`,
67    they will be passed in `yaml.safe_load` function.
68
69    If you want to use the other yaml package, you can subclass `YamlDeserializer` and implement
70    your own logic.
71    """
72    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:
65def from_yaml(c: Any, s: str, de: type[Deserializer[str]] = YamlDeserializer, **opts: Any) -> Any:
66    """
67    `c` is a class obejct and `s` is YAML string. If you supply keyword arguments other than `de`,
68    they will be passed in `yaml.safe_load` function.
69
70    If you want to use the other yaml package, you can subclass `YamlDeserializer` and implement
71    your own logic.
72    """
73    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, **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    **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    If you want to use the other yaml package, you can subclass `YamlSerializer` and implement
45    your own logic.
46    """
47    return se.serialize(
48        to_dict(obj, c=cls, reuse_instances=reuse_instances, convert_sets=convert_sets), **opts
49    )

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.

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