PHP code example of argo-php / serializer

1. Go to this page and download the library: Download argo-php/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/ */

    

argo-php / serializer example snippets


public function serialize(mixed $data, string $format, ContextBag $contextBag = new ContextBag()): string

public function deserialize(string $data, TypeInterface $type, string $format, ContextBag $contextBag = new ContextBag()): mixed

public function normalize(mixed $data, string $format = null, ContextBag $contextBag = new ContextBag()): array|string|int|float|bool|object|null

public function encode(mixed $data, string $format, ContextBag $contextBag = new ContextBag()): string

public function decode(string $data, string $format, ContextBag $contextBag = new ContextBag()): mixed

public function denormalize(mixed $data, TypeInterface $type, ?string $format = null, ContextBag $contextBag = new ContextBag()): mixed

/** 
 * Here we specify all the normalizers we need.
 * The order of their specification matters, meaning more specific normalizers (such as CarbonNormalizer or BackedEnumNormalizer)
 * must be specified before more general normalizers (such as ObjectNormalizer).
 *
 * Normalizers are created through the container, as some o\Serializer\Normalizer\ArrayableNormalizer::class),
    $container->get(\Argo\Serializer\Normalizer\JsonSerializableNormalizer::class),
    $container->get(\Argo\Serializer\Normalizer\CarbonNormalizer::class),
    $container->get(\Argo\Serializer\Normalizer\BackedEnumNormalizer::class),
    $container->get(\Argo\Serializer\Normalizer\ObjectNormalizer::class),
    $container->get(\Argo\Serializer\Normalizer\BuiltinDenormalizer::class),
];

/**
 * Here we specify all the encoders we need. Please note that to use them, you must install the corresponding packages.
 */
$encoders = [
    new \Argo\Serializer\JsonEncoder\JsonEncoder(), // available in the argo/json-encoder package
    new \Argo\Serializer\XmlEncoder\XmlEncoder(), // available in the argo/xml-encoder package
];

/**
 * This is a validator instance to support data validation before denormalization.
 * Can be omitted if validation is not needed.
 */
$validator = $container->make(\Argo\Serializer\Validator\SerializerValidatorInterface::class);

// это объект сериализатора. Через него будут производиться все дальнейшие действия по сериализации/десериализации
$serializer = new \Argo\Serializer\Serializer\Serializer($normalizers, $encoders, $validator);

class Person
{
    public function __construct(
        public int $age,
        public string $name,
        public bool $sportsperson
    ) {
    }
}

$person = new Person(39, 'Jane Doe', false);
/** @var \Argo\Serializer\Serializer\Serializer $serializer */
$jsonContent = $serializer->serialize($person, 'json');
// $jsonContent contains {"name":"Jane Doe","age":39,"sportsperson":false}

$object = $serializer->deserialize($jsonContent, new \Argo\Types\Atomic\ClassType(Person::class), 'json');
// $object - This is an object of the Person class with the age, name, and sportsperson fields filled in.