diff --git a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php index d4070c23040d..8925ec36f7f9 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonDecode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonDecode.php @@ -108,7 +108,7 @@ public function decode($data, $format, array $context = array()) } if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { - throw new UnexpectedValueException(json_last_error_message()); + throw new UnexpectedValueException(json_last_error_msg()); } return $decodedData; diff --git a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php index aa7d64530e14..454c0d6a1147 100644 --- a/src/Symfony/Component/Serializer/Encoder/JsonEncode.php +++ b/src/Symfony/Component/Serializer/Encoder/JsonEncode.php @@ -56,7 +56,7 @@ public function encode($data, $format, array $context = array()) $encodedJson = json_encode($data, $context['json_encode_options']); if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { - throw new UnexpectedValueException(json_last_error_message()); + throw new UnexpectedValueException(json_last_error_msg()); } return $encodedJson; diff --git a/src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php b/src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php new file mode 100644 index 000000000000..03f2187b5576 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Encoder/JsonDecodeTest.php @@ -0,0 +1,44 @@ + + * + * 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\JsonDecode; + +class JsonDecodeTest extends \PHPUnit_Framework_TestCase +{ + /** @var \Symfony\Component\Serializer\Encoder\JsonDecode */ + private $decoder; + + protected function setUp() + { + $this->decoder = new JsonDecode(true); + } + + public function testDecodeWithValidData() + { + $json = json_encode(array( + 'hello' => 'world', + )); + $result = $this->decoder->decode($json, 'json'); + $this->assertEquals(array( + 'hello' => 'world', + ), $result); + } + + /** + * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException + */ + public function testDecodeWithInvalidData() + { + $result = $this->decoder->decode('kaboom!', 'json'); + } +}