Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ce41542

Browse filesBrowse files
committed
Add a normalizer that support JsonSerializable objects
1 parent 8f3c06b commit ce41542
Copy full SHA for ce41542

File tree

4 files changed

+134
-0
lines changed
Filter options

4 files changed

+134
-0
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Bundle/FrameworkBundle/Resources/config/serializer.xml
+6Lines 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" />
+42Lines 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+
}
+30Lines 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+
}
+56Lines 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' => '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)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.