unpacking GVariant in javascript

匿名 (未验证) 提交于 2019-12-03 01:45:01

问题:

I have an array stored as a GVariant of type a(ss) in GSettings, that I want to use in a Cinnamon Applet. I can retrieve the value successfully using the following code:

let schema = schema_source.lookup(SCHEMA_NAME, false); let settings = new Gio.Settings({ settings_schema: schema }); let my_value = settings.get_value('myvalue');

but I can't unpack it. As far as I can see, I will probably need to unpack it using a GVariantIter structure, but the documentation is limited, and I can't find the correct interface in the gjs API (if, indeed, it exists). Does anyone know how to do it?

Thanks!

edit: my schema looks like this:

<key type="a(ss)" name="myvalue">     <default>[]</default>     <summary>an array of (string, string) tuples</summary>     <description></description> </key>

For the time being I'm using an external JSON file to store settings, but it's not a 100% satisfactory solution. I suppose I could maintain two as-type variables, and keep them aligned, but there must be a way to do this properly, right?

回答1:

A bit late, but my_value.unpack() works absolutely fine.

my_value.deep_unpack() will recursively unpack the arrays and their elements.



回答2:

From your type of setting I guess you want to store/retrieve an array of strings? In this case, there is an easier method using Gio.Settings.get_strv(String key):

// Read the array (will create a real JS array): let string_array = settings.get_strv("myvalue"); // Now do something with it... // Store it: settings.set_strv("myvalue", string_array); Gio.Settings.sync(); // Important!

In your schema, you would then include an entry like this:

<key name="myvalue" type="as">   <default>[]</default>   <summary>Some array.</summary>   <description>An Array of strings.</description> </key>

I use the same technique in my extension: Read/Write | Schema



转载请标明出处:unpacking GVariant in javascript
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!