Jump to content




serialize_to_lua


  • You cannot reply to this topic
6 replies to this topic

#1 angellus

  • Members
  • 44 posts

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 by angellus, 07 July 2014 - 06:34 PM.


#2 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 14 May 2014 - 05:35 PM

Great :D This is exactly what i needed for my project - tx for posting this. Hope it gets more recognition.

#3 timia2109

  • Members
  • 71 posts

Posted 15 May 2014 - 06: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 by timia2109, 15 May 2014 - 06:49 PM.


#4 Wojbie

  • Members
  • 631 posts
  • LocationKinda lost

Posted 15 May 2014 - 07: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 :)

#5 angellus

  • Members
  • 44 posts

Posted 07 July 2014 - 06: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.

#6 timia2109

  • Members
  • 71 posts

Posted 03 August 2014 - 02:44 PM

Thanks for the Python Version. My Raspberry would be happy :D

#7 timia2109

  • Members
  • 71 posts

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!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users