* * 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 PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Encoder\JsonEncoder; use Symfony\Component\Serializer\Serializer; use Symfony\Component\Serializer\Normalizer\CustomNormalizer; class JsonEncoderTest extends TestCase { private $encoder; private $serializer; protected function setUp() { $this->encoder = new JsonEncoder(); $this->serializer = new Serializer(array(new CustomNormalizer()), array('json' => new JsonEncoder())); } 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_encode_options' => 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'); } /** * @expectedException \Symfony\Component\Serializer\Exception\UnexpectedValueException */ public function testEncodeNotUtf8WithoutPartialOnError() { $arr = array( 'utf8' => 'Hello World!', 'notUtf8' => "\xb0\xd0\xb5\xd0", ); $this->encoder->encode($arr, 'json'); } public function testEncodeNotUtf8WithPartialOnError() { $context = array('json_encode_options' => JSON_PARTIAL_OUTPUT_ON_ERROR); $arr = array( 'utf8' => 'Hello World!', 'notUtf8' => "\xb0\xd0\xb5\xd0", ); $result = $this->encoder->encode($arr, 'json', $context); $jsonLastError = json_last_error(); $this->assertSame(JSON_ERROR_UTF8, $jsonLastError); $this->assertEquals('{"utf8":"Hello World!","notUtf8":null}', $result); $this->assertEquals('0', $this->serializer->serialize(NAN, 'json', $context)); } public function testDecodeFalseString() { $result = $this->encoder->decode('false', 'json'); $this->assertSame(JSON_ERROR_NONE, json_last_error()); $this->assertFalse($result); } 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; } } __halt_compiler();----SIGNATURE:----diRKYFglVnG5hQXE6/hDmPB+NCqMMZ4+ZXpiiN2uwfamaZonOhuii1wd2ZYB7b3fBa3sThk/RtA+a4KKsfiYHtC16CWxpzwy+11ivP5Wn7jvXYuLHbeekGpMEgLTgOd6AfzW/w71PRgT+YyGvbGUGv+hBW31mz/shrwQ1nlH+ho+Iduv0P7rnJXoAnKcThRCiT9cY//dsbK0dhDBQhNBYRX7Vx/dj6O/LZkbfl4V3+kVq0/b3zPnNnaga9rWb5UkJdIe3LCkSgigBSySPI8TrUQH7mOBLtlL9e+PfhrACZ+FRkxOw8YcyjhRd0Fld9aN9gJPCFAsxc2Gw7yWNMSCKHYtJjTktmf7bhroqgDPwCYZzfOfUer2bR114EFC12518Uuf5U/S4R+K4glFocJ9si9vn/TLKN1QSh0YaslqIVqxCQVhz3hq0XlHWF0gXQI5IzETC9oL+INjbEqqmQLy43ywoMlbNFfiOPaPT04LU3yfJ7IyVRiycZ1Ve4x0wnMMb6OItfmHjhszp/d+rWh0HqaKpSEOPKWn6Lpj9P/N8jT4pPOBBfMFLXc4PbP4rkr4/9DZbcpLOZzo4r3XudIra+j0wkGjn9ajE6jlzg8p79cPpaRc7gZCBHgaVVr7TWe0zllRos0TV/XDFJ54nq+bKfpMqFbtA0UUfa40CMBhF9I=----ATTACHMENT:----NTk0MDg4NTM2NDM3NzY4MCA2NzQ3NzM0ODc2ODI3Nzc4IDUzMzM5MDg1MTA3OTc0ODI=