使用 C++ 将 DBUS 响应类型解析为字典或映射数组

问题描述 投票:0回答:1

我正在使用 C++ 代码开发 Linux DBUS API,尝试运行 dbus 方法并解析响应消息 typeof (a{sv}) 但遇到了一些问题。我正在使用 glib 和 glibmm 库。

这是我的代码:

    const auto call_result = proxy -> call_sync("GetConfiguration",
    tuple, 1000);

std::cout << "Contents of call_result: " << call_result.print() <<
    std::endl;
std::cout << "call_result type: " <<
    call_result.get_type_string() << std::endl;

Glib::Variant < std::vector < std::map < Glib::ustring, Glib::VariantBase >>> testConfigVect;

call_result.get_child(testConfigVect, 0);
std::cout << "testConfigVect type: " <<
    testConfigVect.get_type_string() << std::endl;

std::size_t numChildren = testConfigVect.get_n_children();
std::cout << "numChildren: " << numChildren << std::endl;

for (std::size_t i = 0; i < numChildren; i++) {

    auto mapa = testConfigVect.get_child(i);
    const char * typeName = typeid(mapa).name();
    std::cout << "Type of mapa: " << typeName << std::endl;

    if (mapa.empty()) {
        std::cout << "Map is empty." << std::endl;
    } else {
        std::cout << "Map is not empty." << std::endl;
    }

    for (const auto & pair: mapa) {
        const Glib::ustring & key = pair.first;
        const Glib::VariantBase & value = pair.second;

        std::cout << "Key: " << key << ", Value: " << value.print() << std::endl;
    }
}

我发现了一个空地图,但我可以看到有效的 dbus 响应是带有地图信息的 {sv} 格式。 奇怪为什么我看不到这里的地图! 这是我的代码的输出:

Contents of call_result: ({'SecureBoot': <'False'>},) 
call_result type: (a{sv}) 
testConfigVect type: a{sv} 
numChildren: 1 
Type of mapa:
St3mapIN4Glib7ustringENS0_11VariantBaseESt4lessIS1_ESaISt4pairIKS1_S2_EEE
Map is empty.

感谢您的帮助!

c++ linux glib dbus glibmm
1个回答
0
投票

这里的困惑似乎在于

a{sv}
:这不是一个字典数组,它只是一个字典。因此,如果您将 testConfigVect 视为地图,那么它可能会开始工作。

© www.soinside.com 2019 - 2024. All rights reserved.