This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
angellus's profile picture

serialize_to_lua

Started by angellus, 23 March 2014 - 09:35 PM
angellus #1
Posted 23 March 2014 - 10:35 PM
So, I realize dumping a PHP array to JSON and then decoding the JSON in ComputerCraft is not the most efficient way to send data from a server to ComputerCraft. Because of that, I wrote this simple function to serialize a PHP array to a format that textutils.unseralize can handle. It will output it in the same exact format as textutils.seralize.


function serialize_to_lua($data) {
  $lua_table = "{";
  foreach ($data as $index => $value) {
	$lua_table .= (is_numeric($index) ? "[".$index."]=" : "[\"".$index."\"]=");
	if (is_array($value)) {
	  $lua_table .= serialize_to_lua($value);
	}
	else if (is_bool($value)) {
	  $lua_table .= ($value ? "true" : "false");
	}
	else if (is_numeric($value)) {
	  $lua_table .= $value;
	}
	else {
	  $lua_table .= "\"".$value."\"";
	}
	$lua_table .= ",";
  }
  $lua_table .= "}";
  return $lua_table;
}

EDIT: PYTHON!

def serialize_to_lua(data):
  def serialize(key, value):
	lua_table = (("["+str(key)+"]=") if isinstance(key, int) else ("[\"" + str(key) + "\"]="))
	if (isinstance(value, (list, dict))):
	  lua_table += recursion(value)
	elif (isinstance(value, bool)):
	  lua_table += (("true") if value else ("false"))
	elif (isinstance(value, int)):
	  lua_table += str(value)
	else:
	  lua_table += "\"" + str(value) + "\""
	lua_table += ","
	return lua_table
  def recursion(data):
	lua_table = "{"
	if (isinstance(data, dict)):
	  for key, value in data.iteritems():
		lua_table += serialize(key, value)
	else:
	  for index, value in enumerate(data):
		lua_table += serialize(index, value)
    lua_table += "}"
    return lua_table
  return recursion(data)
Edited on 07 July 2014 - 05:34 PM
Wojbie #2
Posted 14 May 2014 - 06:35 PM
Great :D/> This is exactly what i needed for my project - tx for posting this. Hope it gets more recognition.
timia2109 #3
Posted 15 May 2014 - 07:39 PM
Have created a way to udo it. Have fun!


function unserialize_from_lua($pString)
{
$Temp = $pString;
$lang = strlen($Temp);
$Temp = substr($Temp, 1,$lang-2);
$Temp = str_ireplace(array('[',']','='),array('','',':'), $Temp);
$Temp = str_ireplace(array('{','}'), array('[',']'), $Temp);
$Temp = '{'.$Temp.'}';
return json_decode($Temp,true);
}
Edited on 15 May 2014 - 05:49 PM
Wojbie #4
Posted 15 May 2014 - 08:41 PM
Tx :D/> I was planning to make this one but if you are volunteering your time who i am to not use it :P/>
That will speed what i am planning to do up a lot :)/>
angellus #5
Posted 07 July 2014 - 07:29 PM
I just added a Python version of the code. I finally decided to migrate my autoupdater backend to match the rest of my personal Web site.
timia2109 #6
Posted 03 August 2014 - 03:44 PM
Thanks for the Python Version. My Raspberry would be happy :D/>
timia2109 #7
Posted 05 January 2015 - 12:30 AM
I edit it a few time ago, so that it support Objects and now it can be formatet in HTML (for debugging)
use to_lua(Object/array, formatHTML(true/false));
but to_lua does also work!
http://pastebin.com/n8Tm3acV

Enjoy!