TL;DR Encoding a large table to json and then decoding it turns the index into a string, such that I can’t do myTable[1][15], it has to be myTable[1][“15”] instead.
I have a multi dimensional table with up to 15 rows and columns that I want to encode using json. It’s essentially a sparse matrix so not every coordinate contains information.
Here is an example of my table:
{{[3] = {object = {name = “wall”}}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
json.encode() will produce something like this which is fine and works well when it’s decoded.
[[null,null,{“object”:{“name”:“wall”}}],[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
The problem comes when I have table like this:
{{[15] = {object = {name = “wall”}}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
Instead of filling in the missing values with null like the previous example, json.encode() produces this:
[{“15”:{“object”:{“name”:“wall”}}},[],[],[],[],[],[],[],[],[],[],[],[],[],[]]
But when it gets decoded it turns into this:
{{[“15”] = {object = {name = “wall”}}}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}, {}}
Meaning that I can’t access that object using myTable[1][15], because the index has been turned into a string. I have to access it using myTable[1][“15”].
Obviously this is not ideal. Am I doing something wrong? Is there some way to gracefully handle this?