wizbii / json-serializer
Package info
gitlab.com/wizbii-open-source/json-serializer-bundle
Type:symfony-bundle
pkg:composer/wizbii/json-serializer
2.0.4
2026-03-11 12:54 UTC
Requires
- php: ^8.1
- ext-json: *
- psr/log: ^3
- symfony/config: ^6.4|^7|^8
- symfony/dependency-injection: ^6.4|^7|^8
- symfony/http-kernel: ^6.4|^7|^8
- symfony/yaml: ^6.4|^7|^8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3
- phpstan/phpstan: ^1|^2
- phpunit/phpunit: ^13
README
Objectives
This package aims to help you serialize and deserialize objects into JSON with maximum performances. To do so, no reflection is used and the developer must implement 2 methods for each objects. It depends on php 7.4+ and Symfony 5.0+
Installation
composer require wizbii/json-serializer-bundle
Usage
First, create an object that implement ArraySerializable interface:
use Wizbii\JsonSerializerBundle\ArraySerializable;
class SimpleSerializableObject implements ArraySerializable
{
private string $foo;
public function serialize(): array
{
return [
'foo' => $this->foo,
];
}
public static function deserialize(array $contentAsArray)
{
return (new SimpleSerializableObject())->setFoo($contentAsArray['foo'] ?? '');
}
public function getFoo(): string
{
return $this->foo;
}
public function setFoo(string $foo): SimpleSerializableObject
{
$this->foo = $foo;
return $this;
}
}
Then, use the Serializer service to (de)serialize it:
class MyController
{
private Serializer $serializer;
public function __construct(Serializer $serializer) {
$this->serializer = $serializer;
}
public function getSimpleObjectAction() {
$simpleObject = (new SimpleObject())->setFoo('bar');
return new Response($this->serializer->serialize($simpleObject));
}
public function getFoo() {
$content = '{"foo":"bar"}';
$simpleObject = $this->serializer->deserialize($content, SimpleObject::class);
return new Response($simpleObject->getFoo());
}
}
Contribute
- Fork the repository
- Make your changes
- Test them with
composer dev:checks(it will run test, phpstan and cs:lint subcommands) - Create a Merge Request