public function testTwoLevelObjectDefault() { $obj = new stdClass(); $obj->developer = new stdClass(); $obj->developer->name = 'Yada'; $this->assertEquals('default', Json::objectGet($obj, 'developer.name.notexist', 'default')); }
public function testMoreComplexJson() { $this->assertTrue(Json::isJson('{ "status" : "success", "data" : { "post" : { "id" : 1, "title" : "A blog post", "body" : "Some useful content" } } }')); }
public function testCreateFactoryMethod() { $json = Json::create(); $this->assertTrue($json instanceof Json); }
<?php require __DIR__ . '/vendor/autoload.php'; use Yadakhov\Json; // Using get and set with dot notation $json = new Json(['class' => 'Json', 'data' => ['name' => 'Yada Khov', 'job' => 'developer']]); echo $json . PHP_EOL; /* {"class":"Json","data":{"name":"Yada Khov","job":"developer"}} */ // Use dot notation to access second level elements $json->set('data.name', 'Liam Neeson'); $json->set('data.job', 'actor'); echo $json->toStringPretty() . PHP_EOL; /* { "class": "Json", "data": { "name": "Liam Neeson", "job": "actor" } } */
<?php require __DIR__ . '/vendor/autoload.php'; use Yadakhov\Json; // Using PHP objects to instantiate a new json object $dateTime = new DateTime(); var_dump($dateTime); /* object(DateTime)#2 (3) { ["date"]=> string(19) "2015-06-08 06:26:11" ["timezone_type"]=> int(3) ["timezone"]=> string(16) "America/New_York" } */ $json = new Json($dateTime); echo $json->toStringPretty(); /* { "date": "2015-06-08 06:26:11", "timezone_type": 3, "timezone": "America\/New_York" } */ var_dump($json->toArray());