Skip to content

Commit

Permalink
feat(handler): Create JsonSerializable handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcin Czarnecki committed May 4, 2023
1 parent b4e6128 commit 9c5e363
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
15 changes: 15 additions & 0 deletions src/Handler/JsonSerializableHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Handler;

use JMS\Serializer\Visitor\SerializationVisitorInterface;

final class JsonSerializableHandler
{
public function __invoke(SerializationVisitorInterface $visitor, \JsonSerializable $object)
{
return $object->jsonSerialize();
}
}
10 changes: 9 additions & 1 deletion tests/Benchmark/AbstractSerializationBench.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace JMS\Serializer\Tests\Benchmark;

use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Serializer;
use JMS\Serializer\SerializerBuilder;
Expand Down Expand Up @@ -46,7 +47,10 @@ abstract class AbstractSerializationBench

public function __construct()
{
$this->serializer = SerializerBuilder::create()->build();
$this->serializer = SerializerBuilder::create()
->configureHandlers(\Closure::fromCallable([$this, 'configureHandlers']))
->addDefaultHandlers()
->build();
$this->collection = $this->createCollection();
$this->format = $this->getFormat();
}
Expand Down Expand Up @@ -89,4 +93,8 @@ private function createPost()

return $post;
}

protected function configureHandlers(HandlerRegistryInterface $handlerRegistry)
{
}
}
24 changes: 24 additions & 0 deletions tests/Benchmark/Performance/JsonSerializableBench.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Benchmark\Performance;

use JMS\Serializer\GraphNavigatorInterface;
use JMS\Serializer\Handler\HandlerRegistryInterface;
use JMS\Serializer\Handler\JsonSerializableHandler;
use JMS\Serializer\Tests\Benchmark\AbstractSerializationBench;
use JMS\Serializer\Tests\Fixtures\Author;

class JsonSerializableBench extends AbstractSerializationBench
{
protected function getFormat(): string
{
return 'json';
}

protected function configureHandlers(HandlerRegistryInterface $handlerRegistry)
{
$handlerRegistry->registerHandler(GraphNavigatorInterface::DIRECTION_SERIALIZATION, Author::class, 'json', new JsonSerializableHandler());
}
}
9 changes: 8 additions & 1 deletion tests/Fixtures/Author.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;

class Author
class Author implements \JsonSerializable
{
/**
* @Type("string")
Expand All @@ -26,4 +26,11 @@ public function getName()
{
return $this->name;
}

public function jsonSerialize()
{
return [
'json_full_name' => $this->name,
];
}
}
30 changes: 30 additions & 0 deletions tests/Handler/JsonSerializableHandlerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Handler;

use JMS\Serializer\Handler\JsonSerializableHandler;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Visitor\SerializationVisitorInterface;
use PHPUnit\Framework\TestCase;

final class JsonSerializableHandlerTest extends TestCase
{
/** @var JsonSerializableHandler */
private $handler;

public function testSerialize(): void
{
$data = new Author('scyzoryck');

$serialized = ($this->handler)($this->createMock(SerializationVisitorInterface::class), $data);

$this->assertEquals(['json_full_name' => 'scyzoryck'], $serialized);
}

protected function setUp(): void
{
$this->handler = new JsonSerializableHandler();
}
}

0 comments on commit 9c5e363

Please sign in to comment.