dbus-native
dbus-native copied to clipboard
How do I call a method with signature a{sv}
I have a separate application which exposes a DBus interface with a few methods I would like to call via node-dbus.
One of the methods, func, takes an argument with the DBus signature "a{sv}", and I'm not sure how node-dbus expects such a parameter to be passed. My naive expectation was that I would simply pass a JavaScript object and it would be serialized correctly:
myinterface.func({a: "foo", b: "bar", c: 42}, function(err, result) {...})
and while this does actually call the method, the other application receives an empty array as an argument, so clearly it's not as simple as that.
So what do I do instead?
We really want to allow {a: "foo", b: "bar", c: 42}
to be used for "a{sv}" but at the moment it's quite verbose, you have to go through each step in the tree: top level structure, array, string, variant.
[ // top level struct is js array
[ // array is js array
['a', ['s', 'foo']], // dict_entry is 2 element array - key,value
['b', ['s', 'bar']], // variant is [signature, data]
['c', ['n', 42]]
]
]
As @sidorares said, we are currently working on making it possible to return "real" Javascript object, but this is not yet possible.
I sugges you take a look at this file which is an example file I pushed recently showing how to return some of the most commons types.
Ironically I've just noticed that I have forgotten the variant type! But at least this will give you and idea of how to return an array, a struct, a dict, etc.
When you understand this, just think, as @sidorares suggested, that (variant) = [signature, type]
. So if your variant is actually an int32, it should be represented as ['i', 2392]
. That's all :-)
Has the "return a real javascript object" feature been added yet? Thanks!