def serialize_iter()

in azext_iot/sdk/deviceupdate/dataplane/_serialization.py [0:0]


    def serialize_iter(self, data, iter_type, div=None, **kwargs):
        """Serialize iterable.

        Supported kwargs:
        - serialization_ctxt dict : The current entry of _attribute_map, or same format.
          serialization_ctxt['type'] should be same as data_type.
        - is_xml bool : If set, serialize as XML

        :param list attr: Object to be serialized.
        :param str iter_type: Type of object in the iterable.
        :param bool required: Whether the objects in the iterable must
         not be None or empty.
        :param str div: If set, this str will be used to combine the elements
         in the iterable into a combined string. Default is 'None'.
        :rtype: list, str
        """
        if isinstance(data, str):
            raise SerializationError("Refuse str type as a valid iter type.")

        serialization_ctxt = kwargs.get("serialization_ctxt", {})
        is_xml = kwargs.get("is_xml", False)

        serialized = []
        for d in data:
            try:
                serialized.append(self.serialize_data(d, iter_type, **kwargs))
            except ValueError:
                serialized.append(None)

        if div:
            serialized = ['' if s is None else str(s) for s in serialized]
            serialized = div.join(serialized)

        if 'xml' in serialization_ctxt or is_xml:
            # XML serialization is more complicated
            xml_desc = serialization_ctxt.get('xml', {})
            xml_name = xml_desc.get('name')
            if not xml_name:
                xml_name = serialization_ctxt['key']

            # Create a wrap node if necessary (use the fact that Element and list have "append")
            is_wrapped = xml_desc.get("wrapped", False)
            node_name = xml_desc.get("itemsName", xml_name)
            if is_wrapped:
                final_result = _create_xml_node(
                    xml_name,
                    xml_desc.get('prefix', None),
                    xml_desc.get('ns', None)
                )
            else:
                final_result = []
            # All list elements to "local_node"
            for el in serialized:
                if isinstance(el, ET.Element):
                    el_node = el
                else:
                    el_node = _create_xml_node(
                        node_name,
                        xml_desc.get('prefix', None),
                        xml_desc.get('ns', None)
                    )
                    if el is not None:  # Otherwise it writes "None" :-p
                        el_node.text = str(el)
                final_result.append(el_node)
            return final_result
        return serialized