Serializes the given node to the dictionary

    Serializes the given node to the documented dictionary format.

    :param root_node: Node to serialize
    :returns: The dictionary
    :rtype: dict
486395
def dom_to_dict(root_node):

    

    # Remove namespaces from tagname

    tag = root_node.tagName

    if ":" in tag:

        tag = tag.split(":")[1]

    root_dict = {
        tag: {}
    }

    node_dict = root_dict[tag]

    # Set attributes

    if root_node.hasAttributes():

        for key in list(root_node.attributes.keys()):

            node_dict[key] = root_node.getAttribute(key)

    # Check out child nodes

    for child in root_node.childNodes:

        if child.nodeType == root_node.TEXT_NODE:

            # This is the content

            node_dict['_content'] = child.data

        else:

            subnode_dict = dom_to_dict(child)

            child_tag = child.tagName

            if ":" in child_tag:

                child_tag = child_tag.split(":")[1]

            new_val = subnode_dict[child_tag]

            # If we have several child with same name, put them in a list.

            if child_tag in node_dict:
                prev_val = node_dict[child_tag]

                if type(prev_val) != list:
                    node_dict[child_tag] = [prev_val]

                node_dict[child_tag].append(new_val)

            else:
                node_dict[child_tag] = new_val

    return root_dict
356112
def get_dict(self, u=None):
        
        if u is None:
            return self.get_dict(self.root)
        children = []
        for v in self.adj_list[u]:
            children.append(self.get_dict(v))
        ret = {"name": u, "children": children}
        return ret
800700
def _tree_to_string(cls, root_element, xml_declaration=True, pretty_print=True):
        
        from lxml import etree
        return gf.safe_unicode(etree.tostring(
            root_element,
            encoding="UTF-8",
            method="xml",
            xml_declaration=xml_declaration,
            pretty_print=pretty_print
        ))
864385
def root(self):
        
        if self.is_root:
            return self
        return self.adapter.wrap_node(
            self.adapter.impl_root_element, self.adapter.impl_document,
            self.adapter)
1355228
def xml2dict(root):
    
    output = {}
    if root.items():
        output.update(dict(root.items()))

    for element in root:
        if element:
            if len(element) == 1 or element[0].tag != element[1].tag:
                one_dict = xml2dict(element)
            else:
                one_dict = {ns(element[0].tag): xml2list(element)}

            if element.items():
                one_dict.update(dict(element.items()))
            output.update({ns(element.tag): one_dict})

        elif element.items():
            output.update({ns(element.tag): dict(element.items())})

        else:
            output.update({ns(element.tag): element.text})

    return output
1294154
def etree_to_dict(t, trim=True, **kw):
    
    """
    d = {t.tag: {} if t.attrib else None}
    children = list(t)
    etree_to_dict_w_args = partial(etree_to_dict, trim=trim, **kw)

    if children:
        dd = defaultdict(list)
        d = {t.tag: {}}

        for dc in map(etree_to_dict_w_args, children):
            for k, v in dc.iteritems():
                # do not add Comment instance to the key
                if k is not etree.Comment:
                    dd[k].append(v)

        d[t.tag] = {k: v[0] if len(v) == 1 else v for k, v in dd.iteritems()}

    if t.attrib:
        d[t.tag].update(('@' + k, v) for k, v in t.attrib.iteritems())
    if trim and t.text:
        t.text = t.text.strip()
    if t.text:
        if t.tag is etree.Comment and not kw.get('without_comments'):
            # adds a comments node
            d['#comments'] = t.text
        elif children or t.attrib:
            d[t.tag]['#text'] = t.text
        else:
            d[t.tag] = t.text
    return d
1093182
def to_dict(self):
        
        return {
            "id": self.get_node_id().hex,
            "name": self.title,
            "thumbnail": self.thumbnail.filename if self.thumbnail else None,
            "language" : self.language,
            "description": self.description or "",
            "license": self.license,
            "source_domain": self.source_domain,
            "source_id": self.source_id,
            "ricecooker_version": __version__,
        }
1366836
def serialize_to_string(
        root_processor,  # type: RootProcessor
        value,  # type: Any
        indent=None  # type: Optional[Text]
):
    # type: (...) -> Text
    
    if not _is_valid_root_processor(root_processor):
        raise InvalidRootProcessor('Invalid root processor')

    state = _ProcessorState()
    state.push_location(root_processor.element_path)

    root = root_processor.serialize(value, state)

    state.pop_location()

    # Always encode to UTF-8 because element tree does not support other
    # encodings in earlier Python versions. See: https://bugs.python.org/issue1767933
    serialized_value = ET.tostring(root, encoding='utf-8')

    # Since element tree does not support pretty printing XML, we use minidom to do the pretty
    # printing
    if indent:
        serialized_value = minidom.parseString(serialized_value).toprettyxml(
            indent=indent, encoding='utf-8'
        )

    return serialized_value.decode('utf-8')
142071
def to_dict(self):
        
        obj_dict = super(PlainText, self).to_dict()
        child_dict = {
            'type': self.__class__.__name__,
            'text': self.text
        }
        obj_dict.update(child_dict)
        return obj_dict
1073706
def dict_to_xml(xml_dict):
    
    import lxml.etree as etree
    root_tag = list(xml_dict.keys())[0]
    root = etree.Element(root_tag)
    _dict_to_xml_recurse(root, xml_dict[root_tag])
    return root
109746
def _to_dict(self, serialize=False):
        
        copy_dict = self.__dict__.copy()
        for key, value in vars(self).items():
            # We want to send all ids to Zendesk always
            if serialize and key == 'id':
                continue

            # If this is a Zenpy object, convert it to a dict.
            if not serialize and isinstance(value, BaseObject):
                copy_dict[key] = copy_dict.pop(key).to_dict()

            # This object has a flag indicating it has been dirtied, so we want to send it off.
            elif serialize and getattr(value, '_dirty', False):
                continue

            # Here we have an attribute that should always be sent to Zendesk.
            elif serialize and key in self._always_dirty:
                continue

            # These are for internal tracking, so just delete.
            elif key in ('api', '_dirty_attributes', '_always_dirty', '_dirty_callback', '_dirty'):
                del copy_dict[key]

            # If the attribute has not been modified, do not send it.
            elif serialize and key not in self._dirty_attributes:
                del copy_dict[key]

            # Some reserved words are prefixed with an underscore, remove it here.
            elif key.startswith('_'):
                copy_dict[key[1:]] = copy_dict[key]
                del copy_dict[key]
        return copy_dict
1280592
def serialize_table(ctx, document, table, root):
    

    # What we should check really is why do we pass None as root element
    # There is a good chance some content is missing after the import

    if root is None:
        return root

    if ctx.ilvl != None:
        root = close_list(ctx, root)
        ctx.ilvl, ctx.numid = None, None

    _table = etree.SubElement(root, 'table')
    _table.set('border', '1')
    _table.set('width', '100%')

    style = get_style(document, table)

    if style:
        _table.set('class', get_css_classes(document, style))

    for rows in table.rows:
        _tr = etree.SubElement(_table, 'tr')

        for cell in rows:
            _td = etree.SubElement(_tr, 'td')

            if cell.grid_span != 1:
                _td.set('colspan', str(cell.grid_span))

            if cell.row_span != 1:
                _td.set('rowspan', str(cell.row_span))

            for elem in cell.elements:
                if isinstance(elem, doc.Paragraph):
                    _ser = ctx.get_serializer(elem)
                    _td = _ser(ctx, document, elem, _td, embed=False)

            if ctx.ilvl != None:
#                root = close_list(ctx, root)
                _td = close_list(ctx, _td)

                ctx.ilvl, ctx.numid = None, None

            fire_hooks(ctx, document, table, _td, ctx.get_hook('td'))
        fire_hooks(ctx, document, table, _td, ctx.get_hook('tr'))

    fire_hooks(ctx, document, table, _table, ctx.get_hook('table'))

    return root
1225806
def CopyToDict(self):
    
    dictionary = {}
    for attribute_name, attribute_value in self.GetAttributes():
      if attribute_value is None:
        continue

      dictionary[attribute_name] = attribute_value

    return dictionary
158774
def to_dict(self, serialize=False):
        
        if serialize:
            encode_method = json_encode_for_zendesk
        else:
            encode_method = json_encode_for_printing
        return json.loads(json.dumps(self._to_dict(serialize=serialize), default=encode_method))
915378
def root(self):
        
        node = self
        while node.package is not None:
            node = node.package
        return node
1159474
def _serialize_xml(self, root):
        
        if hasattr(etree, 'tostringlist'):
            if six.PY3:
                xml_content_list = [
                    x.decode("utf-8") for x in etree.tostringlist(root)]
            else:
                xml_content_list = etree.tostringlist(root)

            xml = '\r\n'.join(xml_content_list) + '\r\n'
        else:
            if six.PY3:
                xml_content = etree.tostring(root).decode("utf-8")
            else:
                xml_content = etree.tostring(root)
            xml = xml_content + '\r\n'
        return xml
684255
def _serialize(self, uri, node):
        
        meta = self._decode_meta(node['meta'], is_published=bool(node['is_published']))
        return {
            'uri': uri.clone(ext=node['plugin'], version=node['version']),
            'content': node['content'],
            'meta': meta
        }