It is currently April 12th, 2021, 3:32 am

Reading and writing to a JSON-like file

Help with creating, editing & fixing problems with skins
User avatar
Yincognito
Posts: 3052
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Reading and writing to a JSON-like file

Post by Yincognito »

I have a large (over 5 MB) UTF-8 (in Notepad++) encoded "text" file that I would like to (selectively) read from and write to, while also quickly iterating over its "records" in Rainmeter. The file structure is similar to a JSON/Javascript one, like this (classes are more like "objects", but you get the idea):

Code: Select all

MasterClass
{
Class1 : ClassName1 {
 Field1: Value1
 Field2: Value2
}

Class2 : ClassName2 {
 Field1: Value1
 Field2: Value2
 Field3: Value3
}

Class3 : ClassName3 {
 Field1: Value1
}

Class4 : ClassName4 {
 Field1: Value1
 Field2: Value2
 Field3: Value3
 Field4: Value4
 Field5: Value5
}

Class5 : ClassName5 {
 Field1: Value1
 Field2: Value2
 Field3: Value3
 Field4: Value4
}

}
Now I tried to work it out by using the standard Rainmeter tools, like reading the file with a WebParser measure, iterating through its records by means of dynamic regex quantifiers in String measures, and simulating editing records through InputText measures on top of regular String meters (I can share the code or the original file, but it isn't that important in this context, as what I'm looking after are alternatives, see below):
File Records Manipulation.jpg
The problems are that:
- the size of the file makes iterating through the records relatively slow (I mean, it's fast, but it's not instantaneous like, say, for this mini sample above), with around 236000 lines to parse and around 16000 "classes"/"objects" to deal with
- the particularities of Rainmeter, while enough for reading files and iterating through its records, makes writing the changes back to the same file a bit problematic, partly because of the default INI format, partly because of the overall size, and partly because using external tools like CMD, Powershell, VBScript or others have various drawbacks of their own, like speed, the use of regex, etc.

So, my question is, what alternatives exist to achieve this goal (reading such a file, iterating through its records, selectively change them, and writing the changes back to that file)? Are the already mentioned alternatives using external programs feasible for this scenario, or can it be done fast in Lua? If the latter, can you point me in the right direction or share a minimal script that can do it on the mini sample above (I would probably be able to read and write from/to file in Lua, but - preferably - placing those records in tree-like arrays/tables is a mistery for me at this moment)? Obviously, if that's the case, you can move the topic to the Lua section eventually.

P.S. If anyone wonders, this is about semi-automatically editing a decrypted savegame, instead of doing the operations manually in Notepad++. :D
You do not have the required permissions to view the files attached to this post.