def _do_serialize(struct, fmt, encoding):
res = None<EOL>_check_lib_installed(fmt, '<STR_LIT>')<EOL>if fmt == '<STR_LIT>':<EOL><INDENT>config = configobj.ConfigObj(encoding=encoding)<EOL>for k, v in struct.items():<EOL><INDENT>config[k] = v<EOL><DEDENT>res = b'<STR_LIT:\n>'.join(config.write())<EOL><DEDENT>elif fmt in ['<STR_LIT>', '<STR_LIT>']:<EOL><INDENT>res = (json if fmt == '<STR_LIT>' else json5).dumps(struct,<EOL>indent=<NUM_LIT:2>,<EOL>separators=('<STR_LIT:U+002C>', '<STR_LIT>'),<EOL>ensure_ascii=False).encode(encoding)<EOL><DEDENT>elif fmt == '<STR_LIT>':<EOL><INDENT>if not _is_utf8(encoding):<EOL><INDENT>raise AnyMarkupError('<STR_LIT>')<EOL><DEDENT>res = toml.dumps(struct).encode(encoding)<EOL><DEDENT>elif fmt == '<STR_LIT>':<EOL><INDENT>res = xmltodict.unparse(struct, pretty=True, encoding='<STR_LIT:utf-8>').encode('<STR_LIT:utf-8>')<EOL><DEDENT>elif fmt == '<STR_LIT>':<EOL><INDENT>res = yaml.safe_dump(struct, encoding='<STR_LIT:utf-8>', default_flow_style=False)<EOL><DEDENT>else:<EOL><INDENT>raise  <EOL><DEDENT>return res<EOL>
Actually serialize input.

    Args:
        struct: structure to serialize to
        fmt: format to serialize to
        encoding: encoding to use while serializing
    Returns:
        encoded serialized structure
    Raises:
        various sorts of errors raised by libraries while serializing