Skip to content

# dict

## `xsdata.codegen.mappers.dict`

### `DictMapper`

Bases: `RawDocumentMapper`

Map a dictionary to classes.

This mapper is used to build classes from raw json documents.

Source code in `xsdata/codegen/mappers/dict.py`

    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
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75

    class DictMapper(RawDocumentMapper):
        """Map a dictionary to classes.

        This mapper is used to build classes from raw json documents.
        """

        @classmethod
        def map(cls, data: dict, name: str, location: str) -> list[Class]:
            """Map a dictionary to classes.

            Args:
                data: The json resource data
                name: The main resource name
                location: The resource location

            Returns:
                The list of classes.
            """
            target = cls.build_class(data, name)
            return list(ClassUtils.flatten(target, f"{location}/{name}"))

        @classmethod
        def build_class(cls, data: dict, name: str) -> Class:
            """Build a class from a data dictionary.

            Args:
                data: The json resource data
                name: The main resource name

            Returns:
                The list of classes.
            """
            target = Class(qname=name, tag=Tag.ELEMENT, location="")

            for key, value in data.items():
                cls.build_class_attribute(target, key, value)

            return target

        @classmethod
        def build_class_attribute(cls, target: Class, name: str, value: Any):
            """Build a class attr.

            Args:
                target: The target class instance
                name: The attr name
                value: The data value to extract types and restrictions.
            """
            if isinstance(value, list):
                if not value:
                    cls.build_class_attribute(target, name, None)
                    target.attrs[-1].restrictions.max_occurs = sys.maxsize
                else:
                    for val in value:
                        cls.build_class_attribute(target, name, val)
                        target.attrs[-1].restrictions.max_occurs = sys.maxsize
            else:
                if isinstance(value, dict):
                    inner = cls.build_class(value, name)
                    inner.parent = target
                    attr_type = AttrType(qname=inner.qname, forward=True)
                    target.inner.append(inner)
                else:
                    attr_type = cls.build_attr_type(name, value)

                cls.build_attr(target, name, attr_type, value=value)

#### `map(data, name, location)` `classmethod`

Map a dictionary to classes.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`data` | `dict` | The json resource data | required
`name` | `str` | The main resource name | required
`location` | `str` | The resource location | required

Returns:

Type | Description
--- | ---
`list[Class]` | The list of classes.

Source code in `xsdata/codegen/mappers/dict.py`

    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29

    @classmethod
    def map(cls, data: dict, name: str, location: str) -> list[Class]:
        """Map a dictionary to classes.

        Args:
            data: The json resource data
            name: The main resource name
            location: The resource location

        Returns:
            The list of classes.
        """
        target = cls.build_class(data, name)
        return list(ClassUtils.flatten(target, f"{location}/{name}"))

#### `build_class(data, name)` `classmethod`

Build a class from a data dictionary.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`data` | `dict` | The json resource data | required
`name` | `str` | The main resource name | required

Returns:

Type | Description
--- | ---
`Class` | The list of classes.

Source code in `xsdata/codegen/mappers/dict.py`

    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47

    @classmethod
    def build_class(cls, data: dict, name: str) -> Class:
        """Build a class from a data dictionary.

        Args:
            data: The json resource data
            name: The main resource name

        Returns:
            The list of classes.
        """
        target = Class(qname=name, tag=Tag.ELEMENT, location="")

        for key, value in data.items():
            cls.build_class_attribute(target, key, value)

        return target

#### `build_class_attribute(target, name, value)` `classmethod`

Build a class attr.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`target` | `Class` | The target class instance | required
`name` | `str` | The attr name | required
`value` | `Any` | The data value to extract types and restrictions. | required

Source code in `xsdata/codegen/mappers/dict.py`

    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

    @classmethod
    def build_class_attribute(cls, target: Class, name: str, value: Any):
        """Build a class attr.

        Args:
            target: The target class instance
            name: The attr name
            value: The data value to extract types and restrictions.
        """
        if isinstance(value, list):
            if not value:
                cls.build_class_attribute(target, name, None)
                target.attrs[-1].restrictions.max_occurs = sys.maxsize
            else:
                for val in value:
                    cls.build_class_attribute(target, name, val)
                    target.attrs[-1].restrictions.max_occurs = sys.maxsize
        else:
            if isinstance(value, dict):
                inner = cls.build_class(value, name)
                inner.parent = target
                attr_type = AttrType(qname=inner.qname, forward=True)
                target.inner.append(inner)
            else:
                attr_type = cls.build_attr_type(name, value)

            cls.build_attr(target, name, attr_type, value=value)