PHP code example of monkeyscloud / monkeyslegion-serializer
1. Go to this page and download the library: Download monkeyscloud/monkeyslegion-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/ */
monkeyscloud / monkeyslegion-serializer example snippets
use MonkeysLegion\Serializer\Serializer;
use MonkeysLegion\Serializer\Attribute\{Serializable, SerializedName, Ignore, Groups, Type, DateFormat};
#[Serializable]
class User
{
public function __construct(
public readonly int $id,
public readonly string $name,
#[SerializedName('email_address')]
public readonly string $email,
#[Ignore]
public readonly string $passwordHash,
#[Groups(['admin'])]
public readonly string $role = 'user',
#[DateFormat('Y-m-d')]
public readonly \DateTimeImmutable $createdAt = new \DateTimeImmutable(),
) {}
}
$serializer = Serializer::create();
// Serialize to JSON
$json = $serializer->toJson(new User(
id: 1,
name: 'Jorge',
email: '[email protected]',
passwordHash: 'hashed',
role: 'admin',
));
// {"id":1,"name":"Jorge","email_address":"[email protected]","role":"admin","created_at":"2026-05-17"}
// Deserialize from JSON
$user = $serializer->fromJson($json, User::class);
echo $user->name; // Jorge
use MonkeysLegion\Serializer\Attribute\{PreSerialize, PostDeserialize};
class Order
{
public int $id;
public float $subtotal;
public float $tax;
public float $total = 0;
#[PreSerialize]
public function computeTotal(): void
{
$this->total = $this->subtotal + $this->tax;
}
#[PostDeserialize]
public function onLoaded(): void
{
$this->total = $this->subtotal + $this->tax;
}
}
use MonkeysLegion\Serializer\Attribute\Transform;
class Contact
{
#[Transform(serialize: 'strtolower', deserialize: 'strtolower')]
public string $email;
#[Transform(serialize: 'trim')]
public string $name;
}
use MonkeysLegion\Serializer\Attribute\Accessor;
class User
{
#[Accessor(getter: 'getEmail', setter: 'setEmail')]
private string $email;
public function getEmail(): string { return $this->email; }
public function setEmail(string $v): void { $this->email = strtolower($v); }
}
use MonkeysLegion\Serializer\Attribute\MaxDepth;
class User
{
public int $id;
#[MaxDepth(1)]
public ?Company $company; // Only serialized at top-level, not nested
}
use MonkeysLegion\Serializer\Attribute\{XmlRoot, XmlElement, XmlAttribute};
#[XmlRoot('order')]
class Order
{
#[XmlAttribute]
public int $id;
#[XmlElement('line_item')]
public array $items;
}
$xml = $serializer->toXml($order);
enum Status: string
{
case Active = 'active';
case Inactive = 'inactive';
}
class Account
{
public function __construct(
public readonly int $id,
public readonly Status $status,
) {}
}
// Enums serialize to their scalar value automatically
$json = $serializer->toJson(new Account(1, Status::Active));
// {"id":1,"status":"active"}
// And deserialize back
$account = $serializer->fromJson($json, Account::class);
$account->status === Status::Active; // true
use MonkeysLegion\Serializer\Attribute\Discriminator;
#[Discriminator(field: 'type', map: [
'cat' => Cat::class,
'dog' => Dog::class,
])]
abstract class Animal
{
public string $name;
}
class Cat extends Animal { public int $lives = 9; }
class Dog extends Animal { public string $breed; }
$json = '{"type":"cat","name":"Whiskers","lives":7}';
$animal = $serializer->fromJson($json, Animal::class);
// Returns Cat instance
use MonkeysLegion\Serializer\Attribute\Type;
class Order
{
public int $id;
#[Type('list<OrderItem>')]
public array $items = [];
}
// Serialize/deserialize lists
$json = $serializer->serializeList($orders);
$orders = $serializer->deserializeList($json, Order::class);
use MonkeysLegion\Serializer\Provider\SerializerProvider;
// Register in your container
$serializer = SerializerProvider::register($config['serializer'] ?? []);
bash
php ml serializer:install
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.