Table save key problem

Hi, I am using the code from here to save and load a table of information in my game. If I save a table with information in keys 1-10, then everything works as expected. If I save a value in the 11th key and above, the data does not save. Here is a simplified version of my problem:

local loadsave=require("loadsave") local table1 = {} table1[10]=12 loadsave.saveTable(table1, "myTable.json") table1 = loadsave.loadTable("myTable.json") print(table1[10]) --prints "12"

This works as expected and prints 12. On the other hand this does not work:

local loadsave=require("loadsave") local table1 = {} table1[11]=12 loadsave.saveTable(table1, "myTable.json") table1 = loadsave.loadTable("myTable.json") print(table1[11]) -- prints nil

This prints nil. Could someone explain why I can’t save something in the 11th key alone, or give me an alternative that will allow me to save information like this? Thanks!

Do you have things in table cells 1-9?

Thanks for responding! I don’t have any information in 1-9 in this example. In my game, there is a chance the user won’t either when the table is saved.

you’ve created a “sparse” table - your 11 is not part of a numeric sequence of keys, it’s a standalone single key.  (this is an internal implementation detail of lua, not really important how it works)

but as such, the json library (apparently) seems to think it should be encoded as a string.

(prove it to yourself by trying this after decoding json:  print( table[“11”] )

that should be considered a bug in the json lib because the type() of that key in the original table is still numeric even though the table is sparse - it’s only json that thinks it should become a string.

That is truly an odd bug.  To get the table save/load code out of the equation run this:

local json = require(“json”)
local table1 = {}
table1[11]=12
local s = json.encode(table1)
print(s)
table1 = json.decode(s)
print(table1[11]) --prints “12”

When you set table1[10] = 12, this is the output:

Nov  8 11:13:26 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: [null,null,null,null,null,null,null,null,null,12]
Nov  8 11:13:26 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: 12

When you set table1[11] to 12, you get this:

Nov  8 11:13:51 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: {“11”:12}
Nov  8 11:13:51 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: nil

I’ll let engineering know.

Oh, can I get the Corona SDK version you’re running?

Thanks for the info. I’m running version 2393 and 2393a

Bug case# 36959

Do you have things in table cells 1-9?

Thanks for responding! I don’t have any information in 1-9 in this example. In my game, there is a chance the user won’t either when the table is saved.

you’ve created a “sparse” table - your 11 is not part of a numeric sequence of keys, it’s a standalone single key.  (this is an internal implementation detail of lua, not really important how it works)

but as such, the json library (apparently) seems to think it should be encoded as a string.

(prove it to yourself by trying this after decoding json:  print( table[“11”] )

that should be considered a bug in the json lib because the type() of that key in the original table is still numeric even though the table is sparse - it’s only json that thinks it should become a string.

That is truly an odd bug.  To get the table save/load code out of the equation run this:

local json = require(“json”)
local table1 = {}
table1[11]=12
local s = json.encode(table1)
print(s)
table1 = json.decode(s)
print(table1[11]) --prints “12”

When you set table1[10] = 12, this is the output:

Nov  8 11:13:26 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: [null,null,null,null,null,null,null,null,null,12]
Nov  8 11:13:26 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: 12

When you set table1[11] to 12, you get this:

Nov  8 11:13:51 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: {“11”:12}
Nov  8 11:13:51 robs-MBP.attlocal.net Corona Simulator[45223] <Warning>: nil

I’ll let engineering know.

Oh, can I get the Corona SDK version you’re running?

Thanks for the info. I’m running version 2393 and 2393a

Bug case# 36959