PHP code example of itav / serializer

1. Go to this page and download the library: Download itav/serializer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

itav / serializer example snippets




use Itav\Component\Serializer\Serializer;

 private $model = 'SomeModel';
    private $color = 'red';
    /**
     * @var Part[]
     */
    private $parts;

    public function setParts($parts)
    {
        $this->parts = $parts;
    }
}
class Part
{
    private $number = 123;
    private $name = 'engine';

}

$car = new Car();
$car->setParts([new Part(), new Part(), new Part()]);

$array = $serializer->normalize($car);
$json = json_encode($array);




$json = <<<JSON
{  
   "model":"MyModel",
   "color":"red",
   "parts":[  
      {  
         "number":123,
         "name":"engine"
      },
      {  
         "number":124,
         "name":"lamp"
      },
      {  
         "number":125,
         "name":"wheel"
      }
   ]
}
JSON;

//$array = json_decode($json, true);

$array = [
    'model' => 'MyModel',
    'color' => 'red',
    'parts' => [
        0 => [
            'number' => 123,
            'name' => 'engine'
        ],
        1 => [
            'number' => 124,
            'name' => 'lamp'
        ],
        2 => [
            'number' => 125,
            'name' => 'wheel'
        ],
    ]
];

$car = $serializer->denormalize($array, Car::class);
bash