How to parse "a(oa{sv})" dbus type?

3.1k Views Asked by At

I am parsing the response to "net.connman.Manager.GetServices" function, which looks like this:

<method name="GetServices">
    <arg name="services" type="a(oa{sv})" direction="out"/>
</method>

which is quite complex structure.

What I got so far is this:

GVariant* result = ... // response containing data
GVariantIter* iter1;
g_variant_get( result, "a(oa{sv})", &iter1 );

GVariant* child = g_variant_iter_next_value( iter1 );
while ( nullptr != child )
{
    gchar* string;
    GVariant* data;
    g_variant_get( child, "(oa{sv})", &string, &data );

    // how to access inner array?

    g_variant_unref( child );
    child = g_variant_iter_next_value( iter1 );
}

g_variant_iter_free( iter1 );

So, how do I access the inner array data?

I tried this: GVariantIter* iter2; g_variant_get( data, "a{sv}", &iter2 ); GVariant* child2 = g_variant_iter_next_value( iter2 );

but it fails with some alignment error:

**
GLib:ERROR:../../glib-2.48.2/glib/gvarianttypeinfo.c:163:g_variant_type_info_check: assertion failed: (info->alignment == 0 || info->alignment == 1 || info->alignment == 3 || info->alignment == 7)
Aborted
1

There are 1 best solutions below

0
On BEST ANSWER

data should have type GVariantIter*, not GVariant*, as per the documentation for GVariant Format Strings (you are passing a GVariant format string as the second argument to g_variant_get()).

You can simplify the code quite a bit by using g_variant_iter_loop() though:

/* Compile with: `gcc `pkg-config --cflags --libs glib-2.0 gio-2.0` -o test test.c`.
 * Public domain. */

#include <glib.h>
#include <gio/gio.h>

int
main (void)
{
  g_autoptr(GVariant) result = g_variant_new_parsed ("@a(oa{sv}) [('/', { 'hello': <'test'>})]");

  g_autoptr(GVariantIter) iter1 = NULL;
  g_variant_get (result, "a(oa{sv})", &iter1);

  const gchar *string;
  g_autoptr(GVariantIter) iter2 = NULL;

  while (g_variant_iter_loop (iter1, "(&oa{sv})", &string, &iter2))
    {
      const gchar *key;
      g_autoptr(GVariant) value = NULL;

      while (g_variant_iter_loop (iter2, "{&sv}", &key, &value))
        g_message ("%s, %s:%p", string, key, value);
    }

  return 0;
}