Example #1
0
<?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());