8000 Add a normalizer that support JsonSerializable objects · symfony/symfony@ce41542 · GitHub
[go: up one dir, main page]

Skip to content

Commit ce41542

Browse files
committed
Add a normalizer that support JsonSerializable objects
1 parent 8f3c06b commit ce41542

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed

src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@
2626
<tag name="serializer.normalizer" priority="-1000" />
2727
</service>
2828

29+
<!-- Json Normalizer -->
30+
<service id="serializer.normalizer.json" class="Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer" public="false">
31+
<!-- Run after all custom serializers, but before the ObjectNormalizer -->
32+
<tag name="serializer.normalizer" priority="-900" />
33+
</service>
34+
2935
<!-- Loader -->
3036
<service id="serializer.mapping.chain_loader" class="Symfony\Component\Serializer\Mapping\Loader\LoaderChain" public="false">
3137
<argument type="collection" />
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Normalizer;
13+
14+
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
15+
16+
/**
17+
* A normalizer that uses an objects own JsonSerializable implementation.
18+
*
19+
* @author Fred Cox <mcfedr@gmail.com>
20+
*/
21+
class JsonSerializableNormalizer implements NormalizerInterface
22+
{
23+
/**
24+
* {@inheritdoc}
25+
*/
26+
public function normalize($object, $format = null, array $context = [])
27+
{
28+
if (!$object instanceof \JsonSerializable) {
29+
throw new InvalidArgumentException('The object must implement "\JsonSerializable".');
30+
}
31+
32+
return $object->jsonSerialize();
33+
}
34+
35+
/**
36+
* {@inheritdoc}
37+
*/
38+
public function supportsNormalization($data, $format = null)
39+
{
40+
return $data instanceof \JsonSerializable;
41+
}
42+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
class JsonSerializableDummy implements \JsonSerializable
15+
{
16+
public $foo = 'a';
17+
public $bar = 'b';
18+
public $baz = 'c';
19+
public $qux = 'd';
20+
21+
public function jsonSerialize()
22+
{
23+
return [
24+
'foo' => $this->foo,
25+
'bar' => $this->bar,
26+
'baz' => $this->baz,
27+
'qux' => $this->qux,
28+
];
29+
}
30+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Normalizer;
13+
14+
use Symfony\Component\Serializer\Normalizer\JsonSerializableNormalizer;
15+
use Symfony\Component\Serializer\Tests\Fixtures\JsonSerializableDummy;
16+
17+
/**
18+
* @author Fred Cox <mcfedr@gmail.com>
19+
*/
20+
class JsonSerializableNormalizerTest extends \PHPUnit_Framework_TestCase
21+
{
22+
/**
23+
* @var JsonSerializableNormalizer
24+
*/
25+
private $normalizer;
26+
27+
protected function setUp()
28+
{
29+
$this->normalizer = new JsonSerializableNormalizer();
30+
}
31+
32+
public function testSupportNormalization()
33+
{
34+
$this->assertTrue($this->normalizer->supportsNormalization(new JsonSerializableDummy()));
35+
$this->assertFalse($this->normalizer->supportsNormalization(new \stdClass()));
36+
}
37+
38+
public function testNormalize()
39+
{
40+
$this->assertEquals([
41+
'foo' => 'a',
42+
'bar' => 'b',
43+
'baz' => 'c',
44+
'qux 76A7 ' => 'd',
45+
], $this->normalizer->normalize(new JsonSerializableDummy()));
46+
}
47+
48+
/**
49+
* @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException
50+
* @expectedExceptionMessage The object must implement "\JsonSerializable".
51+
*/
52+
public function testInvalidDataThrowException()
53+
{
54+
$this->normalizer->normalize(new \stdClass());
55+
}
56+
}

0 commit comments

Comments
 (0)
0