Skip to content

# config

## `xsdata.formats.dataclass.serializers.config`

### `SerializerConfig` `dataclass`

Serializer configuration options.

Not all options are applicable for both xml and json documents.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`encoding` | `str` | Text encoding | `'UTF-8'`
`xml_version` | `str` | XML Version number (1.0|1.1) | `'1.0'`
`xml_declaration` | `bool` | Generate XML declaration | `True`
`indent` | `str | None` | Indent output by the given string | `None`
`ignore_default_attributes` | `bool` | Ignore optional attributes with default values | `False`
`schema_location` | `str | None` | xsi:schemaLocation attribute value | `None`
`no_namespace_schema_location` | `str | None` | xsi:noNamespaceSchemaLocation attribute value | `None`
`globalns` | `dict[str, Callable] | None` | Dictionary containing global variables to extend or overwrite for typing | `None`

Source code in `xsdata/formats/dataclass/serializers/config.py`

     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    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

    @dataclass
    class SerializerConfig:
        """Serializer configuration options.

        Not all options are applicable for both xml and json documents.

        Args:
            encoding: Text encoding
            xml_version: XML Version number (1.0|1.1)
            xml_declaration: Generate XML declaration
            indent: Indent output by the given string
            ignore_default_attributes: Ignore optional attributes with default values
            schema_location: xsi:schemaLocation attribute value
            no_namespace_schema_location: xsi:noNamespaceSchemaLocation attribute value
            globalns: Dictionary containing global variables to extend or
                overwrite for typing
        """

        encoding: str = "UTF-8"
        xml_version: str = "1.0"
        xml_declaration: bool = True
        indent: str | None = None
        ignore_default_attributes: bool = False
        schema_location: str | None = None
        no_namespace_schema_location: str | None = None
        globalns: dict[str, Callable] | None = None

        # Deprecated
        pretty_print: InitVar[bool] = False
        pretty_print_indent: InitVar[str | None] = None

        def __post_init__(self, pretty_print: bool, pretty_print_indent: str | None):
            """Handle deprecated pretty print/indent behaviour."""
            if pretty_print:
                self.__setattr__("pretty_print", pretty_print)
            if pretty_print_indent:
                self.__setattr__("pretty_print_indent", pretty_print_indent)

        def __setattr__(self, key: str, value: Any):
            """Handle deprecated pretty print/indent behaviour."""
            if key == "pretty_print":
                warnings.warn(
                    "Setting `pretty_print` is deprecated, use `indent` instead",
                    DeprecationWarning,
                )
                self.indent = "  "
            elif key == "pretty_print_indent":
                warnings.warn(
                    "Setting `pretty_print_indent` is deprecated, use `indent` instead",
                    DeprecationWarning,
                )
                self.indent = value
            else:
                super().__setattr__(key, value)

#### `__post_init__(pretty_print, pretty_print_indent)`

Handle deprecated pretty print/indent behaviour.

Source code in `xsdata/formats/dataclass/serializers/config.py`

    38
    39
    40
    41
    42
    43

    def __post_init__(self, pretty_print: bool, pretty_print_indent: str | None):
        """Handle deprecated pretty print/indent behaviour."""
        if pretty_print:
            self.__setattr__("pretty_print", pretty_print)
        if pretty_print_indent:
            self.__setattr__("pretty_print_indent", pretty_print_indent)

#### `__setattr__(key, value)`

Handle deprecated pretty print/indent behaviour.

Source code in `xsdata/formats/dataclass/serializers/config.py`

    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60

    def __setattr__(self, key: str, value: Any):
        """Handle deprecated pretty print/indent behaviour."""
        if key == "pretty_print":
            warnings.warn(
                "Setting `pretty_print` is deprecated, use `indent` instead",
                DeprecationWarning,
            )
            self.indent = "  "
        elif key == "pretty_print_indent":
            warnings.warn(
                "Setting `pretty_print_indent` is deprecated, use `indent` instead",
                DeprecationWarning,
            )
            self.indent = value
        else:
            super().__setattr__(key, value)