g_variant_equal() and dictionaries

For anyone using g_variant_equal() with a type containing a dictionary, you should the aware that, somewhat unexpectedly in my opinion, g_variant_equal() only returns true if (and only if) the keys in the dictionary are in the same order (GVariant implements dictionaries as an array of key-value pairs).

You can resolve this by using a function that recursively checks for semantic equivalence (such as this one).

I’m sure I’m not going to be the only person to find this useful. I’ve filed this as bug #622590.

About Danielle

Danielle is an Australian software engineer, computer scientist and feminist. She has worked on GNOME for some time now. Opinions and writing are solely her own and so not represent her employer, the GNOME Foundation, or anyone else but herself.
This entry was posted in example code, gvariant, hacking, programming, work. Bookmark the permalink.

2 Responses to g_variant_equal() and dictionaries

  1. Did Ryan elaborate on why the order is important? Because this sounds like a bug in g_variant_equal() to me. And being able to memcmp() two variants does not sound like a good enough excuse to me.

  2. Danielle says:

    Nothing that seemed really convincing to me. The main use for g_variant_equal() seems to be GHashTables, but you can only use simple variants (i.e. of type ‘?’) with g_variant_hash() anyway.

    This kind of makes sense if you’re expanding a dictionary variant (i.e. of type a{?*}) into a GHashTable (which I think should also be a method provided by GLib), but in the more generic comparison case, there should be a method call for that.

Comments are closed.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>