I'm curious as to how this would be causing problems.
The library I'm using is here:
http://regex.info/code/JSON.luaBoth the encode and decode methods check the following
function OBJDEF:encode(value, etc)
if type(self) ~= 'table' or self.__index ~= OBJDEF then
OBJDEF:onEncodeError("JSON:encode must be called in method format", etc)
end
The second condition fails in computercraft.
The second condition exists to make sure that the method is called correctly. You're supposed to call it with
json:encode(tabletoencode)
But you could call it accidentally with
json.encode(tabletoencode)
If you did that the method would carry on happily thinking that tabletoencode was self, rather than json. It would then explode with a confusing error. The check prevents that.
So why does it fail in computercraft? Well it makes sure it's called the right way by confirming self.__index == OBJDEF. This works because earlier on OBJDEF.__index was explicity set to OBJDEF, then set as self's metatable. But under computercraft when self's metatable is set, OBJDEF.__index is converted to a function, and suddenly self.__index and OBJDEF reference completely different things.
Wow that was a mouthful. I'll just give you the code in the spoiler. It returns the json object when you load the script with json = (loadfile "JSON.lua")()
Spoiler
local VERSION = 20130120.6 -- version history at end of file
local OBJDEF = { VERSION = VERSION }
...
OBJDEF.onDecodeOfNilError = OBJDEF.onDecodeError
OBJDEF.onDecodeOfHTMLError = OBJDEF.onDecodeError
...
OBJDEF.__index = OBJDEF
function OBJDEF:new(args)
local new = { }
if args then
for key, val in pairs(args) do
new[key] = val
end
end
return setmetatable(new, OBJDEF)
end
return OBJDEF:new()