How can I read a value from an array of dictionaries stored in GSettings with Vala?

415 views Asked by At

The GSettings Vala docs are not very detailed and I can't find a way to read an item from an array of dictionaries stored in GSettings. Right now I have this in my schema:

<key type="aa{ss}" name="services">
    <default>[{"id": "postgresql", "name": "PostgreSQL"}]</default>
    <summary></summary>
    <description></description>
</key>

How can I read the values for the keys id and name of the first value of the services array? I tried a lot of possibilities (using get_value("services") then get_child(0), for example) to read the dictionary but it simply breaks and I don't know how to debug it.

2

There are 2 answers

0
Talysson On

I discovered the problem was that somehow my settings instance wasn't being initialized correctly when created inside a static construct block. Moving the creation of that to a memoized method solved the problem.

1
Michael Gratton On

Your format string aa{ss} is for an "array of array of dicts of string keys and string values" --- note the double array. You probably want just a{ss}, then you can access the dicts via get_child(0) as you were doing, then get lookup the values from the variant returned from that using lookup_value(...)