Marshalling to dbus dict

John (J5) Palmieri johnp at redhat.com
Thu Mar 6 14:52:36 PST 2008


On Thu, 2008-03-06 at 17:21 +0100, Sanel Zukan wrote:
> Hi to all,
> 
> I'm working on dbus c++ binding for EDE (Edbus) and seems that I got 
> stucked with the complex types, especially with the dict. To better describe 
> the problem, I will start from the beginning.
> 
> The basic type (please, do not confuse it with dbus "basic type") for
> Edbus is EdbusData. It is a variant-like class that mimicks all known
> dbus types, including a complex ones (variant, struct, dict and array).
> 
> I recently added EdbusDict; it is a map-like container with key/value 
> pairs (each is EdbusData type) and it follows dbus specs: it will accept
> keys as "dbus basic types" and container will not allow duplicate keys in it.
> So, from container side, things are ok. But when comes to the part when
> is needed to fill DBusMessage with container content, some things are
> unclear for me (follows bellow).
> 
> I checked how this is done in libdbus-qt3 (0.8) and this is what I was able
> to find about marshalling a dict (or map in libdbus-qt3) container:
> 
>   // 'it' message iterator is created
> 
>   dict_signature = '{' + key type + value type + '}'
> 
>   // init 'sub' iterator
> 
>   dbus_message_iter_open_container(it, DBUS_TYPE_ARRAY, 
>               dict_signature, sub)
> 
>   for(iterate_over_my_container) {
>       DBusMessageIter item_iterator;
>       dbus_message_iter_open_container(sub, DBUS_TYPE_DICT_ENTRY, 
>               0, item_iterator)
> 
>       // appending via main iterator
>       dbus_message_iter_append_basic(it, key type, key);
> 
>       // appending via item_iterator
>       dbus_message_iter_append_basic(item_iterator, value type, value);
> 
>       dbus_message_iter_close_container(sub, item_iterator);
>    }
> 
>    dbus_message_iter_close_container(...)
> 
> Huh... hoping you will get a clue. Is this the only way dict can be
> represented (because for me it is a bit messy and really don't know what
> is going on) or there exists simpler solution :)?
> 
> At first, I thought dict containers will have a signature 
> 'a{key type val type}' and data would be in form "key val key val", but
> it seems this is not the case. It would be nice if someone provides a
> real example of dict (or map) container signature and how data is
> represented. That would, probably, explain me things a lot :)
> 

Dictionaries are serialized as and array of dict entries.  A dict entry
is just a struct with two values a key and a value (e.g. {key, value}).
What you need to do is iterate over all the keys in a dictionary and for
each key construct a dict entry and append that to the array. To
demarshal you simply iterate over the array and place each key value
pair back into a dictionary.  There is no way for D-Bus to marshal a
dictionary in it's binary form since every language, in fact every dict
implementation, has it's own format at hash algorithm.

Remember, all elements in an array are of the same type so to check if
an array is a dictionary you simply look at the signature the array
contains. If it is a dict entry that array is a dictionary.

-- 
John (J5) Palmieri <johnp at redhat.com>



More information about the dbus mailing list