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

Can't unserialize

Started by timia2109, 19 October 2014 - 10:01 AM
timia2109 #1
Posted 19 October 2014 - 12:01 PM
Hey everyone,
I want to develop an lyrics script. I want to host a few example songs so I use a PHP script and this (http://www.computerc...erialize-to-lua) function to echo a folder list. But CC don't unserialize this table and I don't know why. Maybe anyone can help me.
I don't want to use JSON, because then you need an extra API.

If you want to test it in CC the URL is: http://timia2109.com/ccapi/lyric.php

(formated by me)

{
  [0] = {
	["get"] = "AttentionWhore.txt",
	["Artist"] = "deadmau5",
	["Song"] = "Attention Whore",
  },
  [1] = {
	["get"] = "GoldSkies.txt",
	["Artist"] = "Sander Van Doorn, Martin Garrix and DVBBS",
	["Song"]="Gold Skies",
  },
  [2] = {
	["get"] = "RedLights.txt",
	["Artist"]="Tiesto",
	["Song"]="Red Lights",
  },
  [3] = {
	["get"] = "TheSpark.txt",
	["Artist"] = "Afrojack",
	["Song"]="The Spark",
  },
  [4] = {
	["get"]="Wasted.txt",
	["Artist"]="Tiesto",
	["Song"]="Wasted (feat. Mattew Koma)",
  },
}

Real Output (copy from Chrome so also formated):
Spoiler

{[0]={["get"]="AttentionWhore.txt",["Artist"]="deadmau5",["Song"]="Attention Whore",},[1]={["get"]="GoldSkies.txt",["Artist"]="Sander Van Doorn, Martin Garrix and DVBBS
",["Song"]="Gold Skies
",},[2]={["get"]="RedLights.txt",["Artist"]="Tiesto
",["Song"]="Red Lights
",},[3]={["get"]="TheSpark.txt",["Artist"]="Afrojack
",["Song"]="The Spark
",},[4]={["get"]="Wasted.txt",["Artist"]="Tiesto
",["Song"]="Wasted (feat. Mattew Koma)
",},}

PHP Code:
Spoiler

<?php
define("URL","http://timia2109.com/ccapis/lyric.php");
function to_lua($data) {
  $lua_table = "{";
  foreach ($data as $index => $value) {
		$lua_table .= (is_numeric($index) ? "[".$index."]=" : "[\"".$index."\"]=");
		if (is_array($value)) {
		  $lua_table .= 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;
}
if (isset($_GET["song"])) {
echo file_get_contents("lyrics/".$_GET["song"]);
}
else {
$rawSongs = scandir("lyrics");
$rtn = array();
foreach ($rawSongs as $k => $v) {
  if ($v != "." and $v != "..") {
   $s = split("\n",file_get_contents("lyrics/".$v));
   $rtn[] = array("get" => $v,"Artist" => $s[0], "Song" => $s[1]);
  }
}
echo to_lua($rtn);
}

Thank you and sorry for bad english,
timia2109
Edited on 19 October 2014 - 10:05 AM
Bomb Bloke #2
Posted 19 October 2014 - 12:28 PM
This works for me:

local webHandle = http.get("http://timia2109.com/ccapi/lyric.php")

if not webHandle then error("Unable to connect to server!") end

local myTable = textutils.unserialise(webHandle.readAll())

webHandle.close()
timia2109 #3
Posted 19 October 2014 - 12:45 PM
Ok my error. I test it in a Emulator and there was the problem. On CC everything is working. But thanks!