JSON encoding differently than it decodes.

So i have a multi-dimensional lua table and when I encode it then write it to a file, it encodes differently than it decodes. Example:

[lua]local t = {}
t[1] = {}
t[1][1] = {}
t[1][1].data = “string”
local contents = json.encode( t )

local newTable = json.decode(contents)
local data = newTable[“1”][1].data --I have to wrap the first dimension around quotes in order
–to access the data. But before it’s encoded I don’t. If I don’t
–wrap it in quotes, it throws an error.[/lua]

I’m using trial build 704 so I tried manually including the new dkjson library into my project but it still doesn’t work. [import]uid: 63320 topic_id: 25671 reply_id: 325671[/import]

The first thing I notice is your trying to access t[1][1] without initializing it first. Try doing this and see if you can access the newTable without quotes.

local t = {}  
t[1] = {}  
t[1][1].data = "string"  

[import]uid: 56820 topic_id: 25671 reply_id: 103783[/import]

I actually already did that. I clipped that part out when I posted the code. It works perfectly fine except when I load it from json. [import]uid: 63320 topic_id: 25671 reply_id: 103786[/import]

Sorry, should of been taken further. Each one needs initialized first before the data will be stored correctly.

local t = {}  
t[1] = {}  
t[1][1] = {}  
t[1][1].data = "string"  

Then the rest should work without quotes. [import]uid: 56820 topic_id: 25671 reply_id: 103790[/import]

Well my code was set up that way as well but it still didn’t work. What I ended up doing was converting the first dimension to a string across my whole project. Example:

[lua]table = {}
for i = 1,10 do
table[tostring(i)] ={}
for j=1, 10 do
table[tostring(i)][j] = {}
table[tostring(i)][j].data = “string”
end
end[/lua]
Maybe not the most efficient but at least it works. Thanks for the help though! [import]uid: 63320 topic_id: 25671 reply_id: 103793[/import]

I’ve run across the same issue, but I’ve only seen it in situations where the tables being serialized (and reconstituted) are of mixed type. The json in this case seems to be of the form “1”:value, “2”:value, etc… but only sometimes. It was quite infuriating. I wrote the following code that I run on the reconstituted tables that will restore both the indexed elements and the keyed elements:

function utils.fixJSONData(originalTable)  
 if type(originalTable) ~= "table" then  
 return originalTable  
 end  
  
 local newTable = {}  
 for key, value in pairs(originalTable) do  
 if not tonumber(key) then  
 newTable[key] = utils.fixJSONData(value)  
 end  
 end  
  
 local index = 1  
 while true do  
 local value = originalTable[index]  
 if value then  
 table.insert(newTable, utils.fixJSONData(value))  
 index = index + 1  
 else  
 local stringIndex = tostring(index)  
 local value = originalTable[stringIndex]  
 if value then  
 table.insert(newTable, utils.fixJSONData(value))  
 index = index + 1  
 else  
 break  
 end  
 end  
 end  
  
 return newTable  
end  

This has worked for my cases but I suspect there’ll be some weirdness around the ordering for certain cases given how odd the check for numeric and string numeric indexes is. [import]uid: 117383 topic_id: 25671 reply_id: 104270[/import]

Thanks for that code! I knew I couldn’t be the only one experiencing this issue! Hopefully it gets fixed soon and doesn’t confuse anybody else. [import]uid: 63320 topic_id: 25671 reply_id: 105389[/import]

I’m not sure this can be legitimately fixed. We’re choosing to interpret the json in a particular way that happens to suit Lua (and the data formats we’re expecting) but it could be just as legitimate to have a purely key based table with numeric string keys. At best I could see functionality like this be toggled with a parameter to decode or somesuch. It’s an odd duck. [import]uid: 117383 topic_id: 25671 reply_id: 105478[/import]