Save/Load Dictionary to text file

I want to save the settings of my Mac application in a file equivalent under Windows to .ini files, with key/value pairs. Plist files seem to me too complicated to store a few parameters.
I think I need to use the “Dictionary” class.
But how to save and then load when launching the app this “Dictionary” in a text file? Does anyone have a ready-made routine that will save them time? Is there a better solution? (and especially simpler?)
Thank you for your advice.

Hi Denis,

Have a look at GenerateJSON to turn your dictionary into a string and ParseJSON to turn that string back into a string. Once you have this you can use TextOutputStream & TextInputStream to save & load those strings.

Thank you it looks very interesting. Because is it not possible to save a Dictionary directly in a text file with TextOutput/InputStream ?

Not as that directly.

You can save the Dictionary in a text file using one line for each entry; each entry separated with a \tab (for example).

In a loop, you read one entry from the Dictionary (both values) and save them to the text file.

At read time, you simply do the reverse.

For the location to store the file, may I suggest SpecialFolder.ApplicationData ?

You may read the LR for details (that entry looks draft to me):
SpecialFolder
Only constructive ideas…

PS: don’t flag me, I only try to help.

You can indeed do that, at the condition that the dictionary contains only text :

if diko <> nil then
  dim f as folderItem = SpecialFolder.Applicationdata.child(App.AppDataFolder)
  if not f.Exists then
    f.CreateAsFolder
  end if
  
  If f <> nil and f.exists then
    f = f.child("Diko.csv")
    dim lines() as string
    if Diko.Count > 0 then
      For Each key As Variant In Diko.Keys
        lines.append(key+chr(9)+";"+tab+Diko.Value(key))
      Next
    end if
    
    dim linetowrite as string= join(lines,EndOfLine)
    
    Dim t As TextOutputStream
    If f <> Nil Then
      t = TextOutputStream.Create(f)
      t.WriteLine(linetowrite)
      t.Close
    End If
    
  end if
  
End If
1 Like

Thank you very much this makes me a good starting point !

If the format of the file matters, take a look at my TOML module. It works similar to the JSON functions but the file will look more like an INI. TOML also supports more types, like dates or local times.

Note that with either JSON or TOML, the Dictionary you get back will be case sensitive.

You can find out more about TOML here.

In addition to the other suggestions offered, you might also find this older blog post helpful: