So, I am creating a vim script that should load and parse the JSON file into a local graph of objects. I searched, and I could not find any native way to handle the JSON file, and I do not want to add any dependencies to the script. So I wrote my own function to parse the JSON string (obtained from the file), but it is very slow. At the moment, I repeat each character in the file as follows:
let len = strlen(jsonString) - 1 let i = 0 while i < len let c = strpart(jsonString, i, 1) let i += 1 " A lot of code to process file.... " Note: I've tried short cutting the process by searching for enclosing double-quotes when I come across the initial double quotes (also taking into account escaping '\' character. It doesn't help endwhile
I also tried this method:
for c in split(jsonString, '\zs') " Do a lot of parsing .... endfor
For reference, a file with ~ 29,000 characters takes about 4 seconds to process, which is unacceptable.
Is there a better way to iterate over a string in a vim script?
Or even better, did I miss the native function for parsing JSON?
Update:
I did not request any dependencies because I:
- I did not want to deal with them.
- I really would like to get some ideas for a better way to do this if someone else is not working.
- Sometimes I just like to do something manually, although the problem has already been solved.
I'm not against plugins or dependencies at all, I'm just curious. So the question is.
I ended up creating my own function to parse a JSON file. I created a script that could parse the package.json file associated with the node.js modules. Because of this, I could rely on a fairly consistent format and stop processing whenever I get the information I need. This usually cuts large chunks of the file, since most developers put the largest fragment of the file, its "readme" section, at the end. Since the package.json file is strictly defined, I left the process somewhat fragile. He suggested the root dictionary { } and is actively looking for specific entries. You can find the script here: https://github.com/ahayman/vim-nodejs-complete/blob/master/after/ftplugin/javascript.vim#L33 .
Of course, this does not answer my question. This is only a solution to my unique problem. I will wait a few days for new answers and pick up the best one before the distribution ends (the alarm is already set on my phone).