Skip to content

bindings

xsdata.formats.bindings

AbstractSerializer

Bases: ABC

Abstract serializer class.

Source code in xsdata/formats/bindings.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
class AbstractSerializer(abc.ABC):
    """Abstract serializer class."""

    @abc.abstractmethod
    def render(self, obj: Any) -> str:
        """Serialize the input model instance to the output string format.

        Args:
            obj: The input model instance to serialize

        Returns:
            The serialized string output format.
        """

render(obj) abstractmethod

Serialize the input model instance to the output string format.

Parameters:

Name Type Description Default
obj Any

The input model instance to serialize

required

Returns:

Type Description
str

The serialized string output format.

Source code in xsdata/formats/bindings.py
12
13
14
15
16
17
18
19
20
21
@abc.abstractmethod
def render(self, obj: Any) -> str:
    """Serialize the input model instance to the output string format.

    Args:
        obj: The input model instance to serialize

    Returns:
        The serialized string output format.
    """

AbstractParser

Bases: ABC

Abstract parser class.

Source code in xsdata/formats/bindings.py
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
class AbstractParser(abc.ABC):
    """Abstract parser class."""

    def from_path(self, path: pathlib.Path, clazz: Optional[Type[T]] = None) -> T:
        """Parse the input file into the target class type.

        If no clazz is provided, the binding context will try
        to locate it from imported dataclasses.

        Args:
            path: The path to the input file
            clazz: The target class type to parse the file into

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.parse(str(path.resolve()), clazz)

    def from_string(self, source: str, clazz: Optional[Type[T]] = None) -> T:
        """Parse the input source string into the target class type.

        If no clazz is provided, the binding context will try
        to locate it from imported dataclasses.

        Args:
            source: The source string to parse
            clazz: The target class type to parse the source string into

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.from_bytes(source.encode(), clazz)

    def from_bytes(self, source: bytes, clazz: Optional[Type[T]] = None) -> T:
        """Parse the input source bytes object into the target class type.

        If no clazz is provided, the binding context will try
        to locate it from imported dataclasses.

        Args:
            source: The source bytes object to parse
            clazz: The target class type to parse the source bytes object

        Returns:
            An instance of the specified class representing the parsed content.
        """
        return self.parse(io.BytesIO(source), clazz)

    @abc.abstractmethod
    def parse(self, source: Any, clazz: Optional[Type[T]] = None) -> T:
        """Parse the input file or stream into the target class type.

        If no clazz is provided, the binding context will try
        to locate it from imported dataclasses.

        Args:
            source: The source stream object to parse
            clazz: The target class type to parse the source bytes object

        Returns:
            An instance of the specified class representing the parsed content.
        """

from_path(path, clazz=None)

Parse the input file into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:

Name Type Description Default
path Path

The path to the input file

required
clazz Optional[Type[T]]

The target class type to parse the file into

None

Returns:

Type Description
T

An instance of the specified class representing the parsed content.

Source code in xsdata/formats/bindings.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def from_path(self, path: pathlib.Path, clazz: Optional[Type[T]] = None) -> T:
    """Parse the input file into the target class type.

    If no clazz is provided, the binding context will try
    to locate it from imported dataclasses.

    Args:
        path: The path to the input file
        clazz: The target class type to parse the file into

    Returns:
        An instance of the specified class representing the parsed content.
    """
    return self.parse(str(path.resolve()), clazz)

from_string(source, clazz=None)

Parse the input source string into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:

Name Type Description Default
source str

The source string to parse

required
clazz Optional[Type[T]]

The target class type to parse the source string into

None

Returns:

Type Description
T

An instance of the specified class representing the parsed content.

Source code in xsdata/formats/bindings.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def from_string(self, source: str, clazz: Optional[Type[T]] = None) -> T:
    """Parse the input source string into the target class type.

    If no clazz is provided, the binding context will try
    to locate it from imported dataclasses.

    Args:
        source: The source string to parse
        clazz: The target class type to parse the source string into

    Returns:
        An instance of the specified class representing the parsed content.
    """
    return self.from_bytes(source.encode(), clazz)

from_bytes(source, clazz=None)

Parse the input source bytes object into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:

Name Type Description Default
source bytes

The source bytes object to parse

required
clazz Optional[Type[T]]

The target class type to parse the source bytes object

None

Returns:

Type Description
T

An instance of the specified class representing the parsed content.

Source code in xsdata/formats/bindings.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def from_bytes(self, source: bytes, clazz: Optional[Type[T]] = None) -> T:
    """Parse the input source bytes object into the target class type.

    If no clazz is provided, the binding context will try
    to locate it from imported dataclasses.

    Args:
        source: The source bytes object to parse
        clazz: The target class type to parse the source bytes object

    Returns:
        An instance of the specified class representing the parsed content.
    """
    return self.parse(io.BytesIO(source), clazz)

parse(source, clazz=None) abstractmethod

Parse the input file or stream into the target class type.

If no clazz is provided, the binding context will try to locate it from imported dataclasses.

Parameters:

Name Type Description Default
source Any

The source stream object to parse

required
clazz Optional[Type[T]]

The target class type to parse the source bytes object

None

Returns:

Type Description
T

An instance of the specified class representing the parsed content.

Source code in xsdata/formats/bindings.py
72
73
74
75
76
77
78
79
80
81
82
83
84
85
@abc.abstractmethod
def parse(self, source: Any, clazz: Optional[Type[T]] = None) -> T:
    """Parse the input file or stream into the target class type.

    If no clazz is provided, the binding context will try
    to locate it from imported dataclasses.

    Args:
        source: The source stream object to parse
        clazz: The target class type to parse the source bytes object

    Returns:
        An instance of the specified class representing the parsed content.
    """