Пример #1
0
 public function testParse()
 {
     $str = "111";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $this->assertEquals(111, $res, "should {$res} == 111");
     $str = "111";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals(111, $res, "should {$res} == 111");
     $str = "111.111";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals(111.111, $res, "should {$res} == 111.111");
     $str = "\"hello\"";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res, "should {$res} == hello");
     $str = "\"he\\\"llo\"";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("he\"llo", $res, "should {$res} == he\"llo");
     $str = "\"he\\'llo\"";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("he'llo", $res, "should {$res} == he'llo");
     $str = "\"he\\nllo\"";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("he\nllo", $res, "should {$res} == he\nllo");
     $str = "{\"key1\":\"hello\"}";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res["key1"], "should " . $res["key1"] . " == hello");
     $str = "{\"key1\":\"hello\", \"key2\":111}";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res["key1"], "should " . $res["key1"] . " == hello");
     $this->assertEquals(111, $res["key2"], "should " . $res["key2"] . " == 111");
     $str = "[\"hello\"]";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res[0], "should " . $res[0] . " == hello");
     $str = "[\"hello\", 111]";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res[0], "should " . $res[0] . " == hello");
     $this->assertEquals(111, $res[1], "should " . $res[1] . " == 111");
     $str = "[{\"key1\":\"hello\", \"key2\": 222},111, \"bye\"]";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals(111, $res[1], "should " . $res[1] . " == 111");
     $this->assertEquals("bye", $res[2], "should " . $res[2] . " == bye");
     $m = $res[0];
     $this->assertEquals("hello", $m["key1"], "should " . $m["key1"] . " == hello");
     $this->assertEquals(222, $m["key2"], "should " . $m["key2"] . " == 222");
     $str = "[{\"key1\":\"hello\", \"key2\": 222},111, \"bye\", [1, 2,3 ]]";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals(111, $res[1], "should " . $res[1] . " == 111");
     $this->assertEquals("bye", $res[2], "should " . $res[2] . " == bye");
     $m = $res[0];
     $this->assertEquals("hello", $m["key1"], "should " . $m["key1"] . " == hello");
     $this->assertEquals(222, $m["key2"], "should " . $m["key2"] . " == 222");
     $l = $res[3];
     $this->assertEquals(1, $l[0], "should " . $l[0] . " == 1");
     $this->assertEquals(2, $l[1], "should " . $l[1] . " == 2");
     $this->assertEquals(3, $l[2], "should " . $l[2] . " == 3");
     $str = "{\"class\":\"grinfeld\\phpjsonable\\utils\\Pair\",\"left\":100,\"right\":\"Test\"}";
     $fp = new StringInputStream($str);
     $result = (new Reader($fp))->parse();
     $this->assertEquals("grinfeld\\phpjsonable\\utils\\Pair", get_class($result), "should " . get_class($result) . " == grinfeld\\phpjsonable\\utils\\Pair");
     $this->assertEquals(100, $result->getLeft(), "should " . $result->getLeft() . " == 100");
     $this->assertEquals("Test", $result->getRight(), "should " . $result->getRight() . " == Test");
     $str = "{\"class\":\"grinfeld.phpjsonable.utils.Pair\",\"left\":100,\"right\":\"Test\"}";
     $fp = new StringInputStream($str, (new Configuration())->push(Configuration::CLASS_TYPE_PROPERTY, LanguageStrategyFactory::LANG_JAVA));
     $result = (new Reader($fp))->parse();
     $this->assertEquals("grinfeld\\phpjsonable\\utils\\Pair", get_class($result), "should " . get_class($result) . " == grinfeld\\phpjsonable\\utils\\Pair");
     $this->assertEquals(100, $result->getLeft(), "should " . $result->getLeft() . " == 100");
     $this->assertEquals("Test", $result->getRight(), "should " . $result->getRight() . " == Test");
     $str = "  {\r\n\"key1\":  \"hello\"\r\n  }";
     $fp = new StringInputStream($str);
     $res = (new Reader($fp))->parse();
     $this->assertEquals("hello", $res["key1"], "should " . $res["key1"] . " == hello");
 }
Пример #2
0
<?php

require "vendor/autoload.php";
$composer = \grinfeld\phpjsonable\parsers\json\Json::decode(new \grinfeld\phpjsonable\utils\streams\StringInputStream(file_get_contents("composer.json")));
$name = preg_replace("/[\\/]/", "_", $composer["name"]);
$sources = array();
if (isset($composer["autoload"]) && isset($composer["autoload"]["psr-4"])) {
    $sources = $composer["autoload"]["psr-4"];
}
$pharName = $name . '.phar';
$phar = new Phar($pharName);
$dirName = dirname(__FILE__);
while (list($suffix, $src) = each($sources)) {
    $dir = $dirName . "\\" . $src;
    $rp = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
    foreach ($rp as $file) {
        if ($file->isFile() && $file->getFilename() != ".." && $file->getFilename() != ".") {
            $pathBefore = substr($file->getPath(), 0, strlen($dir));
            $pathAfter = substr($file->getPath(), strlen($dir) + 1);
            $pathTo = "\\" . $suffix . ($pathAfter !== false ? $pathAfter . "\\" : "") . $file->getFilename();
            $phar->addFromString($pathTo, file_get_contents($file->getPath() . "/" . $file->getFilename()));
        }
    }
}
if (isset($composer["require"])) {
    while (list($library, $ver) = each($composer["require"])) {
        if (file_exists($dirName . "\\vendor\\" . $library)) {
            $dir = $dirName . "\\vendor\\" . $library . "\\src";
            $rp = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir));
        }
        foreach ($rp as $file) {
Пример #3
0
 public function testParse()
 {
     $str = new StringOutputStream();
     Json::encode("string", $str);
     $this->assertEquals("\"string\"", $str->toString(), "Should be \"string\"");
     $str = new StringOutputStream();
     (new Writer($str))->parse("string");
     $this->assertEquals("\"string\"", $str->toString(), "Should be \"string\"");
     $str = "111";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"111\"");
     $str = "111.111";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"111.111\"");
     $str = "\"he\\\"llo\"";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "\"he'llo\"";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "\"he\\nllo\"";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "{\"key1\":\"hello\"}";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "{\"key1\":\"hello\",\"key2\":111,\"key3\":true}";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "[\"hello\"]";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "[\"hello\",111]";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "[{\"key1\":\"hello\",\"key2\":222},111,\"bye\",false]";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals($str, $output->toString(), "Should be \"{$str}\"");
     $str = "[{\"key1\" : \"hello\",\"key2\": 222}, 111, \"bye\",[1, 2, 3]]";
     $fp = new StringInputStream($str);
     $res = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($res, $output);
     $this->assertEquals(str_replace(" ", "", $str), $output->toString(), "Should be \"{$str}\"");
     $str = "{\"left\":100,\"right\":\"Test\",\"class\":\"grinfeld\\phpjsonable\\utils\\Pair\"}";
     $fp = new StringInputStream($str);
     $result = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($result, $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true"));
     $this->assertEquals($str, $output->toString(), "Should be same");
     $str = "{\"left\":100,\"right\":\"Test\",\"class\":\"grinfeld.phpjsonable.utils.Pair\"}";
     $expected = "{\"left\":100,\"right\":\"Test\",\"class\":\"grinfeld.phpjsonable.utils.Pair\"}";
     $fp = new StringInputStream($str);
     $result = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($result, $output, (new Configuration())->push(Configuration::CLASS_TYPE_PROPERTY, LanguageStrategyFactory::LANG_JAVA)->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true"));
     $this->assertEquals($expected, $output->toString(), "Should be {$expected}");
     $str = "{\"left\":100,\"class\":\"grinfeld\\phpjsonable\\utils\\Pair\"}";
     $expected = "{\"left\":100,\"right\":null,\"class\":\"grinfeld\\phpjsonable\\utils\\Pair\"}";
     $fp = new StringInputStream($str);
     $result = Json::decode($fp);
     $output = new StringOutputStream();
     Json::encode($result, $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false"));
     $this->assertEquals($expected, $output->toString(), "Should be {$expected}");
     $expected = "{\"left\":100,\"right\":\"Test\",\"class\":\"grinfeld\\phpjsonable\\utils\\Pair\"}";
     $output = new StringOutputStream();
     $pair = new Pair(100, "Test");
     Json::encode($pair, $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "true")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false"));
     $this->assertEquals($expected, $output->toString(), "Should be {$expected}");
     $output = new StringOutputStream();
     Json::encode(new SimpleBean(), $output, (new Configuration())->push(Configuration::INCLUDE_CLASS_NAME_PROPERTY, "false")->push(Configuration::EXCLUDE_NULL_PROPERTY, "false"));
     $this->assertEquals("{\"val\":\"123\"}", $output->toString(), "Should be 123");
     $date = new DateTime('now');
     $timestamp = $date->getTimestamp();
     $output = new StringOutputStream();
     Json::encode(array("date" => $date), $output);
     $this->assertEquals("{\"date\":{$timestamp}}", $output->toString(), "Should be {$timestamp}");
 }