This is a read-only snapshot of the ComputerCraft forums,
taken in April 2020.
How do I check if a string is a table or not?
Started by Nyarlathotep, 01 August 2013 - 09:44 PMPosted 01 August 2013 - 11:44 PM
I've been working on a banking program, and it relays quite a bit of information over rednet using textutils.serialize and unserialize to send tables. This works fine for the program itself, however it's come to my attention that the server can be crashed by sending a string that is not a table to the computer. Is there a way of checking whether a string is a table or not, or another way of avoiding this problem?
Posted 01 August 2013 - 11:45 PM
Split into new topic.
Posted 02 August 2013 - 12:30 AM
Check if textutils.unserialize returns nil. It is possible to crash textutils.unserialize with "too long without yielding" with a malicious message though.
Posted 02 August 2013 - 12:43 AM
In recent versions, you can simply send the table itself, no serialization necessary.
Posted 02 August 2013 - 01:30 AM
I never knew that. Where is it documented?
Posted 02 August 2013 - 02:08 AM
I just tested with ComputerCraft 1.53 for Minecraft 1.5.2, and you can indeed send raw tables over without needing to processing them.
I don't recall it being documented either, but it's a nice feature nontheless.
I don't recall it being documented either, but it's a nice feature nontheless.
Posted 02 August 2013 - 11:09 AM
Documentation often lags the in-game reality, unfortunately. Changes like these are hard to detect unless they are listed in the "help whatsnew" and I don't know if this change was or not. If someone wanted to do further type testing (I believe numbers and booleans can also be sent) and update the wiki, it would be appreciated.
Posted 02 August 2013 - 01:33 PM
Is that so? Well that's excellent then, thanks for letting me know!
Posted 14 November 2014 - 07:56 AM
Really late to the party on this one but I was struggling with this and couldn't find any solutions. Figured out that while it is serialized you can run string.len(). My empty, not table thing that was making me crash had 3 chars in it so I did if string.len(serializedTable) <= 3 then table = {} end. Works well!
Posted 14 November 2014 - 08:42 AM
If memory serves, textutils.unserialize() simply returns nil if the string can't be converted. So, for what it's worth, I'd run something like this:
local unserialisedTable = textutils.unserialize(serialisedTable) or {}