PHP code example of kariricode / serializer
1. Go to this page and download the library: Download kariricode/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/ */
kariricode / serializer example snippets
// symfony/serializer: 7+ packages, complex config, annotation magic
// league/fractal: transformer boilerplate, no attribute support
// json_encode/decode alone: no groups, no field renaming, no XML/CSV
// Switching from JSON to XML means rewriting your serialization layer entirely.
use KaririCode\Serializer\Provider\SerializerServiceProvider;
$engine = (new SerializerServiceProvider())->createEngine();
// One API, four formats — swap format string to change output
$json = $engine->serialize(['name' => 'Walmir', 'age' => 30], 'json');
$xml = $engine->serialize(['name' => 'Walmir', 'age' => 30], 'xml', ['root' => 'user']);
$csv = $engine->serialize([['name' => 'Walmir', 'age' => 30]], 'csv');
$qs = $engine->serialize(['name' => 'Walmir', 'age' => 30], 'query_string');
// RFC 8259 (JSON) · RFC 4180 (CSV) · RFC 3986 (URL) — built-in compliance
aririCode\Serializer\Provider\SerializerServiceProvider;
$engine = (new SerializerServiceProvider())->createEngine();
// Serialize array to JSON
$result = $engine->serialize(['name' => 'Walmir', 'age' => 30], 'json');
echo $result->getPayload(); // '{"name":"Walmir","age":30}'
// Deserialize JSON to array
$data = $engine->deserialize('{"name":"Walmir"}', 'json');
// ['name' => 'Walmir']
// XML with custom root tag
$result = $engine->serialize(['name' => 'Walmir'], 'xml', ['root' => 'user']);
// <user><name>Walmir</name></user>
use KaririCode\Serializer\Attribute\Serialize;
class UserDto
{
#[Serialize(name: 'user_id', groups: ['admin'])]
public int $id = 42;
#[Serialize(groups: ['public', 'admin'])]
public string $name = 'Walmir Silva';
#[Serialize(ignore: true)]
public string $passwordHash = '...';
}
$provider = new SerializerServiceProvider();
$serializer = $provider->createAttributeSerializer();
// Serialize to JSON (public group)
$json = $serializer->serialize(new UserDto(), 'json', ['public']);
// {"name":"Walmir Silva"}
// Serialize to JSON (admin group)
$json = $serializer->serialize(new UserDto(), 'json', ['admin']);
// {"user_id":42,"name":"Walmir Silva"}
// Deserialize from JSON
$dto = $serializer->deserialize('{"name":"Walmir"}', UserDto::class, 'json');
use KaririCode\Serializer\Contract\Encoder;
final readonly class YamlEncoder implements Encoder
{
public function encode(array $data, SerializationContext $context): string { /* ... */ }
public function decode(string $payload, SerializationContext $context): array { /* ... */ }
public function supports(string $format): bool { return $format === 'yaml'; }
public function getFormat(): string { return 'yaml'; }
}
$registry = $provider->createRegistry();
$registry->register(new YamlEncoder());
$engine = (new SerializerServiceProvider())->createEngine();
$result = $engine->serialize(['name' => 'Walmir', 'age' => 30], 'json');
$result->getPayload(); // '{"name":"Walmir","age":30}'
$result->getFormat(); // 'json'
$result->getPayloadSize(); // byte count
$data = $engine->deserialize('{"name":"Walmir"}', 'json');
// ['name' => 'Walmir']
// CSV with custom separator
$result = $engine->serialize([['a' => 1, 'b' => 2]], 'csv', ['separator' => ';']);