<?php require_once "../vendor/autoload.php"; /* * Client: */ $func = function () { echo "Hallo"; return "World"; }; $ljsonString = \Kanti\LJSON::stringify($func); //Hello echo "\nsend to server -> " . $ljsonString . "\n"; //<br> // send to server -> () => ("World") //<br> /* * Server: */ $functionFromClient = \Kanti\LJSON::parse($ljsonString); echo $functionFromClient(); //World /* * The full Result: */ $fullResult = 'Hallo send to server -> () => ("World") World';
public function testParseLJsonFunctionInFunctionReturnArrayFunction() { $expectedFunction = function ($aaa) { return function () use($aaa) { return [$aaa]; }; }; $actualFunction = LJSON::parse(LJSON::stringify($expectedFunction)); $expectedFunction2 = $expectedFunction(1); $actualFunction2 = $actualFunction(1); $this->assertEquals($expectedFunction2(), $actualFunction2()); }
<?php require_once "../vendor/autoload.php"; // A random object with a pure function inside. $person = ["name" => "John", "mail" => function ($msg) { return ["author" => "John", "message" => $msg]; }]; $personStr = \Kanti\LJSON::stringify($person); $personVal = \Kanti\LJSON::parse($personStr); $mailFunction = $personVal->mail; $mail = $mailFunction("hello"); // would crash with JSON echo $personStr . "\n"; //{"name":"John","mail":(v0) => ({"author":"John","message":v0})} echo \Kanti\LJSON::stringify($mail) . "\n"; //{"author":"John","message":"hello"}
/** * @expectedException \Kanti\StringifyException */ public function testNoErrorHandlerPreset() { if (PHP_MAJOR_VERSION < 5 || PHP_MAJOR_VERSION == 5 && PHP_MINOR_VERSION < 5) { $this->markTestSkipped('only works for php >=5.5'); return; } set_error_handler(null); LJSON::stringify(function ($aaa) { return (double) $aaa; }); }