Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
"justinrainbow/json-schema": ">=1.3.7",
"seld/jsonlint": "~1.0"
},
"require-dev": {
"symfony/serializer": "~2.2|~3.0.0"
},
"suggest": {
"symfony/serializer": "To integrate with the Symfony Serializer."
},
"autoload": {
"psr-4": {
"Webmozart\\Json\\": "src/"
Expand Down
84 changes: 84 additions & 0 deletions src/Symfony/JsonEncoder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

/*
* This file is part of the Webmozart JSON package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* 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 <dunglas@gmail.com>
*/
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['json_schema']) ? $context['json_schema'] : null;

return $this->encoder->encode($data, $schema);
}

/**
* {@inheritdoc}
*/
public function decode($data, $format, array $context = array())
{
$schema = isset($context['json_schema']) ? $context['json_schema'] : null;

return $this->decoder->decode($data, $schema);
}

/**
* {@inheritdoc}
*/
public function supportsEncoding($format)
{
return 'json';
}

/**
* {@inheritdoc}
*/
public function supportsDecoding($format)
{
return 'json';
}
}
97 changes: 97 additions & 0 deletions tests/Symfony/JsonEncoderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of the Webmozart JSON package.
*
* (c) Bernhard Schussek <bschussek@gmail.com>
*
* 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 <dunglas@gmail.com>
*/
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('json_schema' => $this->schemaFile)));
}

public function testEncodeWithSchemaObject()
{
$data = (object) array('name' => 'Kévin');

$this->assertSame('{"name":"K\u00e9vin"}', $this->encoder->encode($data, 'json', array('json_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('json_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('json_schema' => $this->schemaObject));

$this->assertInstanceOf('\stdClass', $data);
$this->assertObjectHasAttribute('name', $data);
$this->assertSame('Kévin', $data->name);
}
}