PHP code example of gamez / ramsey-uuid-normalizer

1. Go to this page and download the library: Download gamez/ramsey-uuid-normalizer 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/ */

    

gamez / ramsey-uuid-normalizer example snippets


use Gamez\Symfony\Component\Serializer\Normalizer\UuidNormalizer;
use Ramsey\Uuid\Uuid;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
use Symfony\Component\Serializer\Serializer;

class Person
{
    public $id;
    public $name;
}

$person = new Person();
$person->id = Uuid::uuid4();
$person->name = 'Jérôme Gamez';

$encoders = [new JsonEncoder()];
$normalizers = [new UuidNormalizer(), new ObjectNormalizer()];
$serializer = new Serializer($normalizers, $encoders);

$json = $serializer->serialize($person, 'json');
echo $json.PHP_EOL;
// {"id":"3d79048c-29e7-482f-979a-5b9a708b2ede","name":"J\u00e9r\u00f4me Gamez"}

$person = $serializer->deserialize($json, Person::class, 'json');
var_dump($person);
/*
class Person#27 (2) {
  public $id =>
  string(36) "3d79048c-29e7-482f-979a-5b9a708b2ede"
  public $name =>
  string(14) "Jérôme Gamez"
}
*/