From b4b2cb96d3b73f1e7150f52d63365af8f94ec2ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 2 Mar 2015 08:37:49 +0100 Subject: [PATCH 1/3] Add a Symfony Serializer bridge --- composer.json | 6 ++ src/Symfony/JsonEncoder.php | 84 ++++++++++++++++++++++++++ tests/Symfony/JsonEncoderTest.php | 97 +++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 src/Symfony/JsonEncoder.php create mode 100644 tests/Symfony/JsonEncoderTest.php diff --git a/composer.json b/composer.json index 64603e4..36926c9 100644 --- a/composer.json +++ b/composer.json @@ -13,6 +13,12 @@ "justinrainbow/json-schema": ">=1.3.7", "seld/jsonlint": "~1.0" }, + "require-dev": { + "symfony/serializer": "~2.0|~3.0.0" + }, + "suggest": { + "symfony/serializer": "To integrate with the Symfony Serializer." + }, "autoload": { "psr-4": { "Webmozart\\Json\\": "src/" diff --git a/src/Symfony/JsonEncoder.php b/src/Symfony/JsonEncoder.php new file mode 100644 index 0000000..662aedf --- /dev/null +++ b/src/Symfony/JsonEncoder.php @@ -0,0 +1,84 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Webmozart\Json\Symfony; + +use Symfony\Component\Serializer\Encoder\DecoderInterface; +use Symfony\Component\Serializer\Encoder\EncoderInterface; +use Webmozart\Json\JsonEncoder as BaseJsonEncoder; +use Webmozart\Json\JsonDecoder as BaseJsonDecoder; +use Webmozart\Json\EncodingFailedException; +use Webmozart\Json\ValidationFailedException; +use Webmozart\Json\InvalidSchemaException; + +/** + * Bridge with the Symfony Serializer Component. + * + * @since 1.0 + * @author Kévin Dunglas + */ +class JsonEncoder implements EncoderInterface, DecoderInterface +{ + /** + * @var BaseJsonEncoder + */ + private $encoder; + /** + * @var BaseJsonDecoder + */ + private $decoder; + + public function __construct(BaseJsonEncoder $encoder = null, BaseJsonDecoder $decoder = null) + { + $this->encoder = $encoder ?: new BaseJsonEncoder(); + $this->decoder = $decoder ?: new BaseJsonDecoder(); + } + + /** + * {@inheritdoc} + * + * @throws EncodingFailedException If the data could not be encoded. + * @throws ValidationFailedException If the data fails schema validation. + * @throws InvalidSchemaException If the schema is invalid. + */ + public function encode($data, $format, array $context = array()) + { + $schema = isset($context['schema']) ? $context['schema'] : null; + + return $this->encoder->encode($data, $schema); + } + + /** + * {@inheritdoc} + */ + public function decode($data, $format, array $context = array()) + { + $schema = isset($context['schema']) ? $context['schema'] : null; + + return $this->decoder->decode($data, $schema); + } + + /** + * {@inheritdoc} + */ + public function supportsEncoding($format) + { + return 'json'; + } + + /** + * {@inheritdoc} + */ + public function supportsDecoding($format) + { + return 'json'; + } +} diff --git a/tests/Symfony/JsonEncoderTest.php b/tests/Symfony/JsonEncoderTest.php new file mode 100644 index 0000000..de18a97 --- /dev/null +++ b/tests/Symfony/JsonEncoderTest.php @@ -0,0 +1,97 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Webmozart\Json\Symfony\Tests; + +use Webmozart\Json\Symfony\JsonEncoder; + +/** + * @since 1.0 + * @author Kévin Dunglas + */ +class JsonEncoderTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var JsonEncoder + */ + private $encoder; + + /** + * @var string + */ + private $fixturesDir; + + /** + * @var string + */ + private $schemaFile; + + /** + * @var \stdClass + */ + private $schemaObject; + + protected function setUp() + { + $this->encoder = new JsonEncoder(); + $this->fixturesDir = __DIR__.'/../Fixtures'; + $this->schemaFile = $this->fixturesDir.'/schema.json'; + $this->schemaObject = json_decode(file_get_contents($this->schemaFile)); + } + + public function testEncode() + { + $data = (object) array('name' => 'Kévin'); + + $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json')); + } + + public function testEncodeWithSchemaFile() + { + $data = (object) array('name' => 'Kévin'); + + $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('schema' => $this->schemaFile))); + } + + public function testEncodeWithSchemaObject() + { + $data = (object) array('name' => 'Kévin'); + + $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('schema' => $this->schemaObject))); + } + + public function testDecode() + { + $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json'); + + $this->assertInstanceOf('\stdClass', $data); + $this->assertObjectHasAttribute('name', $data); + $this->assertSame('Kévin', $data->name); + } + + public function testDecodeWithSchemaFile() + { + $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('schema' => $this->schemaFile)); + + $this->assertInstanceOf('\stdClass', $data); + $this->assertObjectHasAttribute('name', $data); + $this->assertSame('Kévin', $data->name); + } + + public function testDecodeWithSchemaObject() + { + $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('schema' => $this->schemaObject)); + + $this->assertInstanceOf('\stdClass', $data); + $this->assertObjectHasAttribute('name', $data); + $this->assertSame('Kévin', $data->name); + } +} From 64fb80ddb7acf01dd8bb11bb545d61438720a7a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 2 Mar 2015 11:36:05 +0100 Subject: [PATCH 2/3] Use json_schema instead of json in context --- src/Symfony/JsonEncoder.php | 4 ++-- tests/Symfony/JsonEncoderTest.php | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Symfony/JsonEncoder.php b/src/Symfony/JsonEncoder.php index 662aedf..fa92c02 100644 --- a/src/Symfony/JsonEncoder.php +++ b/src/Symfony/JsonEncoder.php @@ -51,7 +51,7 @@ public function __construct(BaseJsonEncoder $encoder = null, BaseJsonDecoder $de */ public function encode($data, $format, array $context = array()) { - $schema = isset($context['schema']) ? $context['schema'] : null; + $schema = isset($context['json_schema']) ? $context['json_schema'] : null; return $this->encoder->encode($data, $schema); } @@ -61,7 +61,7 @@ public function encode($data, $format, array $context = array()) */ public function decode($data, $format, array $context = array()) { - $schema = isset($context['schema']) ? $context['schema'] : null; + $schema = isset($context['json_schema']) ? $context['json_schema'] : null; return $this->decoder->decode($data, $schema); } diff --git a/tests/Symfony/JsonEncoderTest.php b/tests/Symfony/JsonEncoderTest.php index de18a97..a9bc3d6 100644 --- a/tests/Symfony/JsonEncoderTest.php +++ b/tests/Symfony/JsonEncoderTest.php @@ -58,14 +58,14 @@ public function testEncodeWithSchemaFile() { $data = (object) array('name' => 'Kévin'); - $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('schema' => $this->schemaFile))); + $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('json_schema' => $this->schemaFile))); } public function testEncodeWithSchemaObject() { $data = (object) array('name' => 'Kévin'); - $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('schema' => $this->schemaObject))); + $this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('json_schema' => $this->schemaObject))); } public function testDecode() @@ -79,7 +79,7 @@ public function testDecode() public function testDecodeWithSchemaFile() { - $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('schema' => $this->schemaFile)); + $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('json_schema' => $this->schemaFile)); $this->assertInstanceOf('\stdClass', $data); $this->assertObjectHasAttribute('name', $data); @@ -88,7 +88,7 @@ public function testDecodeWithSchemaFile() public function testDecodeWithSchemaObject() { - $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('schema' => $this->schemaObject)); + $data = $this->encoder->decode('{ "name": "K\u00e9vin" }', 'json', array('json_schema' => $this->schemaObject)); $this->assertInstanceOf('\stdClass', $data); $this->assertObjectHasAttribute('name', $data); From 8248df69fda0e0a7f9dd42d6119bfaa390179b0c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Dunglas?= Date: Mon, 2 Mar 2015 11:37:06 +0100 Subject: [PATCH 3/3] Fix symfony/serializer minimum version --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 36926c9..79c31fd 100644 --- a/composer.json +++ b/composer.json @@ -14,7 +14,7 @@ "seld/jsonlint": "~1.0" }, "require-dev": { - "symfony/serializer": "~2.0|~3.0.0" + "symfony/serializer": "~2.2|~3.0.0" }, "suggest": { "symfony/serializer": "To integrate with the Symfony Serializer."