Skip to content

Commit 8278f31

Browse files
committed
Added context to JsonEncoder
1 parent 6fc3d3d commit 8278f31

File tree

4 files changed

+164
-5
lines changed

4 files changed

+164
-5
lines changed

Encoder/JsonDecode.php

Lines changed: 55 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,20 @@
1111

1212
namespace Symfony\Component\Serializer\Encoder;
1313

14+
use Symfony\Component\Serializer\SerializerInterface;
15+
use Symfony\Component\Serializer\SerializerAwareInterface;
16+
1417
/**
1518
* Decodes JSON data
1619
*
1720
* @author Sander Coolen <sander@jibber.nl>
1821
*/
19-
class JsonDecode implements DecoderInterface
22+
class JsonDecode implements DecoderInterface, SerializerAwareInterface
2023
{
2124
private $associative;
2225
private $recursionDepth;
2326
private $lastError = JSON_ERROR_NONE;
27+
protected $serializer;
2428

2529
public function __construct($associative = false, $depth = 512)
2630
{
@@ -50,7 +54,13 @@ public function getLastError()
5054
*/
5155
public function decode($data, $format)
5256
{
53-
$decodedData = json_decode($data, $this->associative, $this->recursionDepth);
57+
$context = $this->getContext();
58+
59+
$associative = $context['associative'];
60+
$recursionDepth = $context['recursionDepth'];
61+
$options = $context['options'];
62+
63+
$decodedData = json_decode($data, $associative, $recursionDepth, $options);
5464
$this->lastError = json_last_error();
5565

5666
return $decodedData;
@@ -63,4 +73,47 @@ public function supportsDecoding($format)
6373
{
6474
return JsonEncoder::FORMAT === $format;
6575
}
76+
77+
/**
78+
* {@inheritdoc}
79+
*/
80+
public function setSerializer(SerializerInterface $serializer)
81+
{
82+
$this->serializer = $serializer;
83+
}
84+
85+
private function getContext()
86+
{
87+
$options = array(
88+
'associative' => $this->associative,
89+
'recursionDepth' => $this->recursionDepth,
90+
'options' => 0
91+
);
92+
93+
if (!$this->serializer) {
94+
return $options;
95+
}
96+
97+
$options = array(
98+
'associative' => false,
99+
'recursionDepth' => 512,
100+
'options' => 0
101+
);
102+
103+
$context = $this->serializer->getContext();
104+
105+
if (isset($context['associative'])) {
106+
$options['associative'] = $context['associative'];
107+
}
108+
109+
if (isset($context['recursionDepth'])) {
110+
$options['recursionDepth'] = $context['recursionDepth'];
111+
}
112+
113+
if (isset($context['options'])) {
114+
$options['options'] = $context['options'];
115+
}
116+
117+
return $options;
118+
}
66119
}

Encoder/JsonEncode.php

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Sander Coolen <sander@jibber.nl>
1818
*/
19-
class JsonEncode implements EncoderInterface
19+
class JsonEncode extends SerializerAwareEncoder implements EncoderInterface
2020
{
2121
private $options ;
2222
private $lastError = JSON_ERROR_NONE;
@@ -48,7 +48,9 @@ public function getLastError()
4848
*/
4949
public function encode($data, $format)
5050
{
51-
$encodedJson = json_encode($data, $this->options);
51+
$options = $this->getContext();
52+
53+
$encodedJson = json_encode($data, $options);
5254
$this->lastError = json_last_error();
5355

5456
return $encodedJson;
@@ -61,4 +63,23 @@ public function supportsEncoding($format)
6163
{
6264
return JsonEncoder::FORMAT === $format;
6365
}
66+
67+
private function getContext()
68+
{
69+
if (!$this->serializer) {
70+
return 0;
71+
}
72+
73+
$context = $this->serializer->getContext();
74+
75+
if (empty($context)) {
76+
$context = array(0);
77+
}
78+
79+
if (!is_array($context)) {
80+
$context = array($context);
81+
}
82+
83+
return array_sum($context);
84+
}
6485
}

Encoder/JsonEncoder.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
*
1717
* @author Jordi Boggiano <j.boggiano@seld.be>
1818
*/
19-
class JsonEncoder implements EncoderInterface, DecoderInterface
19+
class JsonEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface
2020
{
2121
const FORMAT = 'json';
2222

@@ -61,6 +61,8 @@ public function getLastDecodingError()
6161
*/
6262
public function encode($data, $format)
6363
{
64+
$this->encodingImpl->setSerializer($this->serializer);
65+
6466
return $this->encodingImpl->encode($data, self::FORMAT);
6567
}
6668

@@ -69,6 +71,8 @@ public function encode($data, $format)
6971
*/
7072
public function decode($data, $format)
7173
{
74+
$this->decodingImpl->setSerializer($this->serializer);
75+
7276
return $this->decodingImpl->decode($data, self::FORMAT);
7377
}
7478

Tests/Encoder/JsonEncoderTest.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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\Encoder;
13+
14+
use Symfony\Component\Serializer\Encoder\JsonEncoder;
15+
use Symfony\Component\Serializer\Serializer;
16+
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
17+
18+
class JsonEncoderTest extends \PHPUnit_Framework_TestCase
19+
{
20+
protected function setUp()
21+
{
22+
$this->encoder = new JsonEncoder;
23+
$this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
24+
$this->encoder->setSerializer($this->serializer);
25+
}
26+
27+
public function testEncodeScalar()
28+
{
29+
$obj = new \stdClass;
30+
$obj->foo = "foo";
31+
32+
$expected = '{"foo":"foo"}';
33+
34+
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
35+
}
36+
37+
public function testComplexObject()
38+
{
39+
$obj = $this->getObject();
40+
41+
$expected = $this->getJsonSource();
42+
43+
$this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
44+
}
45+
46+
public function testOptions()
47+
{
48+
$context = array(JSON_NUMERIC_CHECK);
49+
50+
$arr = array();
51+
$arr['foo'] = "3";
52+
53+
$expected = '{"foo":3}';
54+
55+
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json', $context));
56+
57+
$arr = array();
58+
$arr['foo'] = "3";
59+
60+
$expected = '{"foo":"3"}';
61+
62+
$this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
63+
}
64+
65+
66+
protected function getJsonSource()
67+
{
68+
return '{"foo":"foo","bar":["a","b"],"baz":{"key":"val","key2":"val","A B":"bar","item":[{"title":"title1"},{"title":"title2"}],"Barry":{"FooBar":{"Baz":"Ed","@id":1}}},"qux":"1"}';
69+
}
70+
71+
protected function getObject()
72+
{
73+
$obj = new \stdClass;
74+
$obj->foo = 'foo';
75+
$obj->bar = array('a', 'b');
76+
$obj->baz = array('key' => 'val', 'key2' => 'val', 'A B' => 'bar', 'item' => array(array('title' => 'title1'), array('title' => 'title2')), 'Barry' => array('FooBar' => array('Baz' => 'Ed', '@id' => 1)));
77+
$obj->qux = "1";
78+
79+
return $obj;
80+
}
81+
}

0 commit comments

Comments
 (0)