Permalink
Browse files

Added context to JsonEncoder

  • Loading branch information...
1 parent 6fc3d3d commit 8278f311b81ca746693535ced5b3c91a206ab14c @dlsniper dlsniper committed Jan 5, 2013
Showing with 164 additions and 5 deletions.
  1. +55 −2 Encoder/JsonDecode.php
  2. +23 −2 Encoder/JsonEncode.php
  3. +5 −1 Encoder/JsonEncoder.php
  4. +81 −0 Tests/Encoder/JsonEncoderTest.php
View
57 Encoder/JsonDecode.php
@@ -11,16 +11,20 @@
namespace Symfony\Component\Serializer\Encoder;
+use Symfony\Component\Serializer\SerializerInterface;
+use Symfony\Component\Serializer\SerializerAwareInterface;
+
/**
* Decodes JSON data
*
* @author Sander Coolen <sander@jibber.nl>
*/
-class JsonDecode implements DecoderInterface
+class JsonDecode implements DecoderInterface, SerializerAwareInterface
{
private $associative;
private $recursionDepth;
private $lastError = JSON_ERROR_NONE;
+ protected $serializer;
public function __construct($associative = false, $depth = 512)
{
@@ -50,7 +54,13 @@ public function getLastError()
*/
public function decode($data, $format)
{
- $decodedData = json_decode($data, $this->associative, $this->recursionDepth);
+ $context = $this->getContext();
+
+ $associative = $context['associative'];
+ $recursionDepth = $context['recursionDepth'];
+ $options = $context['options'];
+
+ $decodedData = json_decode($data, $associative, $recursionDepth, $options);
$this->lastError = json_last_error();
return $decodedData;
@@ -63,4 +73,47 @@ public function supportsDecoding($format)
{
return JsonEncoder::FORMAT === $format;
}
+
+ /**
+ * {@inheritdoc}
+ */
+ public function setSerializer(SerializerInterface $serializer)
+ {
+ $this->serializer = $serializer;
+ }
+
+ private function getContext()
+ {
+ $options = array(
+ 'associative' => $this->associative,
+ 'recursionDepth' => $this->recursionDepth,
+ 'options' => 0
+ );
+
+ if (!$this->serializer) {
+ return $options;
+ }
+
+ $options = array(
+ 'associative' => false,
+ 'recursionDepth' => 512,
+ 'options' => 0
+ );
+
+ $context = $this->serializer->getContext();
+
+ if (isset($context['associative'])) {
+ $options['associative'] = $context['associative'];
+ }
+
+ if (isset($context['recursionDepth'])) {
+ $options['recursionDepth'] = $context['recursionDepth'];
+ }
+
+ if (isset($context['options'])) {
+ $options['options'] = $context['options'];
+ }
+
+ return $options;
+ }
}
View
25 Encoder/JsonEncode.php
@@ -16,7 +16,7 @@
*
* @author Sander Coolen <sander@jibber.nl>
*/
-class JsonEncode implements EncoderInterface
+class JsonEncode extends SerializerAwareEncoder implements EncoderInterface
{
private $options ;
private $lastError = JSON_ERROR_NONE;
@@ -48,7 +48,9 @@ public function getLastError()
*/
public function encode($data, $format)
{
- $encodedJson = json_encode($data, $this->options);
+ $options = $this->getContext();
+
+ $encodedJson = json_encode($data, $options);
$this->lastError = json_last_error();
return $encodedJson;
@@ -61,4 +63,23 @@ public function supportsEncoding($format)
{
return JsonEncoder::FORMAT === $format;
}
+
+ private function getContext()
+ {
+ if (!$this->serializer) {
+ return 0;
+ }
+
+ $context = $this->serializer->getContext();
+
+ if (empty($context)) {
+ $context = array(0);
+ }
+
+ if (!is_array($context)) {
+ $context = array($context);
+ }
+
+ return array_sum($context);
+ }
}
View
6 Encoder/JsonEncoder.php
@@ -16,7 +16,7 @@
*
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
-class JsonEncoder implements EncoderInterface, DecoderInterface
+class JsonEncoder extends SerializerAwareEncoder implements EncoderInterface, DecoderInterface
{
const FORMAT = 'json';
@@ -61,6 +61,8 @@ public function getLastDecodingError()
*/
public function encode($data, $format)
{
+ $this->encodingImpl->setSerializer($this->serializer);
+
return $this->encodingImpl->encode($data, self::FORMAT);
}
@@ -69,6 +71,8 @@ public function encode($data, $format)
*/
public function decode($data, $format)
{
+ $this->decodingImpl->setSerializer($this->serializer);
+
return $this->decodingImpl->decode($data, self::FORMAT);
}
View
81 Tests/Encoder/JsonEncoderTest.php
@@ -0,0 +1,81 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Serializer\Tests\Encoder;
+
+use Symfony\Component\Serializer\Encoder\JsonEncoder;
+use Symfony\Component\Serializer\Serializer;
+use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
+
+class JsonEncoderTest extends \PHPUnit_Framework_TestCase
+{
+ protected function setUp()
+ {
+ $this->encoder = new JsonEncoder;
+ $this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder()));
+ $this->encoder->setSerializer($this->serializer);
+ }
+
+ public function testEncodeScalar()
+ {
+ $obj = new \stdClass;
+ $obj->foo = "foo";
+
+ $expected = '{"foo":"foo"}';
+
+ $this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
+ }
+
+ public function testComplexObject()
+ {
+ $obj = $this->getObject();
+
+ $expected = $this->getJsonSource();
+
+ $this->assertEquals($expected, $this->encoder->encode($obj, 'json'));
+ }
+
+ public function testOptions()
+ {
+ $context = array(JSON_NUMERIC_CHECK);
+
+ $arr = array();
+ $arr['foo'] = "3";
+
+ $expected = '{"foo":3}';
+
+ $this->assertEquals($expected, $this->serializer->serialize($arr, 'json', $context));
+
+ $arr = array();
+ $arr['foo'] = "3";
+
+ $expected = '{"foo":"3"}';
+
+ $this->assertEquals($expected, $this->serializer->serialize($arr, 'json'), 'Context should not be persistent');
+ }
+
+
+ protected function getJsonSource()
+ {
+ 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"}';
+ }
+
+ protected function getObject()
+ {
+ $obj = new \stdClass;
+ $obj->foo = 'foo';
+ $obj->bar = array('a', 'b');
+ $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)));
+ $obj->qux = "1";
+
+ return $obj;
+ }
+}

0 comments on commit 8278f31

Please sign in to comment.