def _do_serialize(struct, fmt, encoding):
    res = None
    _check_lib_installed(fmt, 'serialize')
    if fmt == 'ini':
        config = configobj.ConfigObj(encoding=encoding)
        for k, v in struct.items():
            config[k] = v
        res = b'\n'.join(config.write())
    elif fmt in ['json', 'json5']:
        res = (json if fmt == 'json' else json5).dumps(struct,
                                                       indent=2,
                                                       separators=(',', ': '),
                                                       ensure_ascii=False).encode(encoding)
    elif fmt == 'toml':
        if not _is_utf8(encoding):
            raise AnyMarkupError('toml must always be utf-8 encoded according to specification')
        res = toml.dumps(struct).encode(encoding)
    elif fmt == 'xml':
        res = xmltodict.unparse(struct, pretty=True, encoding='utf-8').encode('utf-8')
    elif fmt == 'yaml':
        res = yaml.safe_dump(struct, encoding='utf-8', default_flow_style=False)
    else:
        raise
    return res