Can't get to array loaded from ModernPreferences

I’m having some trouble reading information out of a preferences file. There is a preference entry that looks to me like an array:

{
    "IgnoredVolumes": [
        "Preboot",
        "VM",
        "Update",
        "home"
    ]
}
 

I can read this into my application using Preferences.Load() (I’m using the ModernPreferences module BTW) but then I can’t seem to get to the item.

  • VarType(Preferences.Get(“IgnoredVolumes”)) returns a 9 (“object”)
  • If I try to read in the array I get TypeMismatchException. Code is:
    var volString As String = String.FromArray(Preferences.Get(“IgnoredVolumes”))
  • If I try to read it into a JSONNode I get a TypeMismatchException. Code here is:
    var myVolumes As JSONNode
    myVolumes = new JSONNode(“IgnoredVolumes”, Preferences.Get(“IgnoredVolumes”).StringValue)

I’ve used ModernPreferences a lot in recent years and it’s generally pretty good. But I’ve only used it to store individual items, not arrays. So it looks like I’m failing to read the array from the preferences file, and I can’t think why.

Can anyone point me to what I’m doing wrong?

Ian.

A workaround might be to stringify the JSONItem before you save it so you can load a string from the preferences and re-JSONify it.

The module you mention is so outdated and unmaintained, the author removed it from GitHub, so we don’t have access to the code to help you improve. There are better solutions, but they do require more work. I had thought maybe writing code wasn’t something you wanted to do at this time.

My personal recommendation would be to scrap the module and write your own. I don’t want a lot going on with a preferences module, so the one I wrote for myself is very lightweight. It’s so basic, it’s not worth sharing – just a few module functions that wrap a private JSONItem property and will automatically load from / write to disk.