GML Saving nested maps to .ini with json_encode and its issues

I

iDecided

Guest
Hey guys, I'm running into an issue and I'm not sure if it's me or GMS2.

I'm using GMS2 and working with saving and loading to an .ini, I'm sure many of you have done this before. I have a map with nested maps and I've added them with ds_map_add_map(). When saving, I use json_encode, write it to the .ini and when I view the file BAM! it's there.

Here's an excerpt from the written ini file:
Code:
[sd_playerstate]
affectionLevelsMap="{ "jacob": 0.000000 }"
The problem comes from when I then try to read that JSON string back into my game. I noticed that json_encode writes the string out with double-quotes around the map keys. This confuses the ini_read_string function because it stores the result from json_encode() in a set of double-quotes. Thus, when I read in the string, it gets cut off prematurely.
When I read it back using ini_read_string(), I get just the first '{' and a space, because it reads the first double-quote of the first key as the end of the string.
Code:
"{ "
I can see that it's writing it correctly, but I also see where it gets confused. So is this me, or is it GMS2?
 
You do not necessarily need to use INI for this, I recommend saving the data in JSON inside a ds_map.
There is a tutorial by Shaun Spalding that teaches you how to do this, I suggest checking out, it is quite useful:

 
You may need to escape the double quotes. See if this works:
Code:
[sd_playerstate]
affectionLevelsMap="{ \"jacob\": 0.000000 }"
If it doesn't work -- or even if it does -- I suggest dropping INIs entirely. With your case, you're already using JSON which can easily accomplish what you're trying to do with INIs without having to use two separate function types:
JSON:
"sd_playerstate": {
    "affectionLevelsMap": { "jacob": 0.000000 },
},
 
Top