A Serializer, but on steroids

Baptiste Leduc

@JoliCode
@Korbeil_

Mathias Arlaud

@bakslashHQ
@matarld

Representing data structures in a format that can be sent or persisted in order to be reconstructed later

Binary, textual

Construction pattern

Databases, flat files, APIs

Anywhere, interoperable

Serialization?

@Korbeil_

@matarld

https://symfony.com/doc/current/components/serializer.html

Symfony's serializer

@Korbeil_

@matarld

Symfony's serializer

{
  "name":"Axel",
  "color":"Red",
  "age":2
}
array(3) {
  ["name"]=> string(4) "Axel"
  ["color"]=> string(3) "Red"
  ["age"]=> int(2)
}
object(Cat)#1 (3) {
  ["name"]=> string(4) "Axel"
  ["color"]=> string(3) "Red"
  ["age"]=> int(2)
}

@Korbeil_

@matarld

Some limitations

@Korbeil_

@matarld

Design & BC limitations

@Korbeil_

@matarld

AbstractNormalizer

allows attributes

instantiates objects

AbstractObjectNormalizer

normalization

denormalization

validation

ObjectNormalizer

extracts attributes

BC policy

🐌

Reflection is slow

Improved over PHP versions, but still...

Lot of caching to improve things

The Reflection limitation

@Korbeil_

@matarld

 Metadata is data

Metadata

@Korbeil_

@matarld

, but about data

data

metadata

"thing"
"animal"
"cat"
"flying cat"

@Korbeil_

@matarld

Metadata

data

/** @temlate T of string */
final class Rain {
  /** @return list<T> */
  public function content(): array {...}
}

/** @var Rain<'cat'|'dog'> $*/
$englishRain = new Rain();

$fallingStuff = $englishRain->content();

@Korbeil_

@matarld

type

no idea
array of something
list of something
list of cats and dogs

Possible JSON

[
  [0.7]
]
{
  "c": 1
}

true

{
  "a": 1
}
[
  3.14
]
[
  "pi"
]
[
  true
]
[
  3.14
]
[
  "dog"
]
[
  "cat"
]
[
  "dog"
]
[
  "dog"
]

The Type limitation

interface PropertyTypeExtractorInterface
{
    /**
     * @return Type[]|null
     */
    public function getTypes(...): ?array;
}

@Korbeil_

@matarld

The Type limitation

interface PropertyTypeExtractorInterface
{
    /**
     * @return Type[]|null
     */
    public function getTypes(...): ?array;
}

only properties

no union/intersection difference

no list/dict difference

no generics

...

@Korbeil_

@matarld

The Type limitation

TypeInfo component

@Korbeil_

@matarld

TypeInfo cool stuff

/**
 * @template T of Animal
 */
final class FlyingAnimal
{
  public function __construct(
    public object $type,
    private int $speed,
  ) {
  }
  
  /**
   * @return class-string<T>
   */
  public function getTypeClass(): string
  {
    return $this->type::class;
  }
  
  public function setSpeed(int $speed): void
  {
  	$this->speed = $speed;
  }
}

PropertyInfo

TypeInfo

@Korbeil_

@matarld

TypeInfo cool stuff

@Korbeil_

@matarld

TypeInfo cool stuff

$resolver = TypeResolver::create();

$resolver->resolve('list<int>');
$resolver->resolve($reflectionClass->getProperty('isFlying'));

@Korbeil_

@matarld

TypeInfo cool stuff

Type::nullable(Type::list(Type::int()));

Type::intersection(
  Type::object(\Stringable::class),
  Type::object(\Traversable::class),
);

Type::generic(
  Type::object(Collection::class),
  Type::object(User::class),
);

@Korbeil_

@matarld

Contribute to TypeInfo!

@Korbeil_

@matarld

The Memory limitation

Huge collection of objects

Super big array

array

@Korbeil_

@matarld

@Korbeil_

@matarld

Json streaming

Huge collection of objects

array

JsonEncoder component

@Korbeil_

@matarld

JsonEncoder component

@Korbeil_

@matarld

PHP

metadata

Cat::class
class Cat
{
  public string $name;
  public bool $flying;
}

// ...

$this->jsonEncoder->encode($cat);

Possible JSON

{
  "name": "any_string",
  "flying: true|false
}

@Korbeil_

@matarld

Encoder

Possible JSON

[  
  {
    "name": "any_string",
    "flying: true|false
  },
  {
    "name": "any_string",
    "flying: true|false
  }
]
return static function ($cats, $stream, $config) {
  $stream->write('[');
  $prefix_0 = '';
  foreach ($cats as $cat) {
    $stream->write($prefix_0);
    $stream->write('{"name":');
    $stream->write(json_encode($cat->name));
    $stream->write(',"flying":');
    $stream->write($cat->flying ? 'true' : 'false');
    $stream->write('}');
    $prefix_0 = ',';
  }
  $stream->write(']');
};

JsonEncoder component

@Korbeil_

@matarld

I want to serialize a cat!

Does an encoder exist?

    /\_____/\
   /  o   o  \
  ( ==  ^  == )
   )         (
  (           )
 ( (  )   (  ) )
(__(__)___(__)__)

Encode the cat

Yep 👌

Compute the cat JSON shape

Generate and store the encoder

No

JsonEncoder component

@Korbeil_

@matarld

Read only needed

JSON part lazily

JsonEncoder pros and cons

Blazing fast!

Flat memory usage

Stream ready

Generics ready

Simple API

Edit data on-the-fly

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  
composer req jolicode/automapper

Mapper

@Korbeil_

@matarld

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  

AutoMapper

final class CustomDogTransformer implements PropertyTransformerInterface, PropertyTransformerSupportInterface
{  
  public function supports(
    TypesMatching $types, 
    SourcePropertyMetadata $source, 
    TargetPropertyMetadata $target, 
    MapperMetadata $mapperMetadata
  ): bool {
    return 
    	$mapperMetadata->source === Cat::class && 
        $mapperMetadata->target === Dog::class &&
        $source->name === 'age';
  }

  public function transform(mixed $cat, object|array $source, array $context): float
  {
    return floor($cat->age * 1.3);
  }
}

@Korbeil_

@matarld

AutoMapper extensions

final class CustomDogAgeTransformer implements PropertyTransformerInterface, PropertyTransformerSupportInterface
{
  public function __construct(
    private AgeConverter $ageConverter,
  ) {}
  
  public function supports(/** ... */): bool
  {
    // same as last slide
  }

  public function transform(mixed $cat, object|array $source, array $context): float
  {
    return $this->ageConverter->fromCatToDog($cat->age);
  }
}

@Korbeil_

@matarld

AutoMapper extensions

final class Cat
{
  #[MapTo(Dog::class, name: 'barkVolume')]
  public int $meowVolume;
        
  #[MapTo(name: 'name')]
  public string $catName;

  #[MapTo('array', ignore: true)]
  public bool $isFlying;
}

@Korbeil_

@matarld

AutoMapper attributes

barkVolume
name
isFlying
meowVolume
name
meowVolume
catName
isFlying
#[MapTo[Dog:class, name: 'barkVolume', transformer: "service('bark_generator').volume()"]
final class Cat
{
  #[MapTo(Dog::class, ignore: true)]
  public int $meowVolume;

  #[MapTo(name: 'name', if: 'instanceof(source) != Cat::class')]
  public string $catName;
  
  #[MapTo(Dog::class, name: 'age', transformer: AgeCustomTransformer::class)]
  public int $age;
}

@Korbeil_

@matarld

AutoMapper attributes

barkVolume
name
age
meowVolume
catName
age
meowVolume
name
age

AutoMapper

@Korbeil_

@matarld

I want to convert a cat to a dog!

Does a mapper exist?

   __
o-''|\_____/)
 \_/|_)     )
    \  __  /
    (_/ (_/  

Convert the cat

Yep 👌

Fetch the metadata

Generate and store the mapper

No

use Symfony\Component\Serializer\SerializerInterface;

final readonly class MyService
{
  public function __construct(
    private SerializerInterface $serializer,
  ) {
  }
}

@Korbeil_

@matarld

The Serializer

TurboSerializer

// config/bundles.php

return [
  // ...
  Mtarld\JsonEncoderBundle\JsonEncoderBundle::class => ['all' => true],
  AutoMapper\Bundle\AutoMapperBundle::class => ['all' => true],
  TurboSerializer\TurboSerializerBundle::class => ['all' => true],
];

@Korbeil_

@matarld

composer require korbeil/turbo-serializer

TurboSerializer

final readonly class ApiController
{
  public function __construct(
    private SerializerInterface $turboSerializer,
  ) {
  }

  #[Route('/api/cat-but-dogs')]
  public function get(): Response
  {
    $cats = $this->callCats();
    
    return new Response($this->turboSerializer->serialize(
      $cats, 
      'json',
      [Serializer::NORMALIZED_TYPE => Type::list(Type::object(Dog::class))]
    ));
  }
}

@Korbeil_

@matarld

Benchmark

@Korbeil_

@matarld

Serialization

TurboSerializer

Serializer

TurboSerializer
Mapped

Deserialization

Serializer

TurboSerializer

TurboSerializer
Mapped

Thanks!

Baptiste Leduc

@JoliCode
@Korbeil_

Mathias Arlaud

@bakslashHQ
@matarld

https://github.com/mtarld/json-encoder-bundle

https://github.com/jolicode/automapper

https://github.com/korbeil/turbo-serializer

https://github.com/korbeil/turbo-serializer-bench

[Symfony Online] A Serializer on steroids

By Mathias Arlaud

[Symfony Online] A Serializer on steroids

  • 718