diff --git a/app/etc/di.xml b/app/etc/di.xml index a44eff21dc86823b8fd132bf1e75adec54fdc767..e430c15729d3d0cced20bf2b326d40f152b174e6 100755 --- a/app/etc/di.xml +++ b/app/etc/di.xml @@ -154,7 +154,7 @@ <preference for="Magento\Framework\EntityManager\MapperInterface" type="Magento\Framework\EntityManager\CompositeMapper"/> <preference for="Magento\Framework\Console\CommandListInterface" type="Magento\Framework\Console\CommandList"/> <preference for="Magento\Framework\DataObject\IdentityGeneratorInterface" type="Magento\Framework\DataObject\IdentityService" /> - <preference for="Magento\Framework\Json\JsonInterface" type="Magento\Framework\Json\Json" /> + <preference for="Magento\Framework\Serialize\SerializerInterface" type="Magento\Framework\Serialize\Serializer\Json" /> <type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" /> <type name="Magento\Framework\Logger\Handler\Base"> <arguments> diff --git a/lib/internal/Magento/Framework/Json/Json.php b/lib/internal/Magento/Framework/Json/Json.php deleted file mode 100644 index c25f71cde4279779e4119b167e1cd47a9d240d41..0000000000000000000000000000000000000000 --- a/lib/internal/Magento/Framework/Json/Json.php +++ /dev/null @@ -1,26 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -namespace Magento\Framework\Json; - -class Json implements JsonInterface -{ - /** - * {@inheritDoc} - */ - public function encode($data, $options = 0) - { - return json_encode($data, $options); - } - - /** - * {@inheritDoc} - */ - public function decode($string, $objectDecodeType = self::TYPE_ARRAY) - { - $result = json_decode($string, $objectDecodeType); - return $result; - } -} diff --git a/lib/internal/Magento/Framework/Json/JsonInterface.php b/lib/internal/Magento/Framework/Json/JsonInterface.php deleted file mode 100644 index 3d9df3d89e7ac3885e0d095916ddfca547332c6e..0000000000000000000000000000000000000000 --- a/lib/internal/Magento/Framework/Json/JsonInterface.php +++ /dev/null @@ -1,38 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -namespace Magento\Framework\Json; - -interface JsonInterface -{ - /** - * Decode object as array - */ - const TYPE_ARRAY = 1; - - /** - * Decode object as object - */ - const TYPE_OBJECT = 0; - - /** - * Encode $data into the JSON format. Please see http://php.net/manual/en/function.json-encode.php for supported - * values for $option - * - * @param array|string $data - * @param int $options - * @return string|bool - */ - public function encode($data, $options = 0); - - /** - * Decode the given string which is encoded in the JSON format - * - * @param string $string - * @param int $objectDecodeType - * @return array|\stdClass - */ - public function decode($string, $objectDecodeType = self::TYPE_ARRAY); -} diff --git a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php b/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php deleted file mode 100644 index 7190e85c5dad5ee01e5b007fdc91f52a09e8bc96..0000000000000000000000000000000000000000 --- a/lib/internal/Magento/Framework/Json/Test/Unit/JsonTest.php +++ /dev/null @@ -1,49 +0,0 @@ -<?php -/** - * Copyright © 2016 Magento. All rights reserved. - * See COPYING.txt for license details. - */ -namespace Magento\Framework\Json\Test\Unit; - -use Magento\Framework\Json\JsonInterface; -use Magento\Framework\Json\Json; - -class JsonTest extends \PHPUnit_Framework_TestCase -{ - /** - * @var Json - */ - private $json; - - protected function setUp() - { - $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->json = $objectManager->getObject(Json::class); - } - - /** - * @param null|bool|array|\stdClass $value - * @param int $objectDecodeType - * @dataProvider encodeDecodeDataProvider - */ - public function testEncodeDecode($value, $objectDecodeType) - { - $this->assertEquals( - $this->json->decode($this->json->encode($value), $objectDecodeType), - $value - ); - } - - public function encodeDecodeDataProvider() - { - $object = new \stdClass(); - $object->a = 'b'; - return [ - ['', JsonInterface::TYPE_ARRAY], - [null, JsonInterface::TYPE_ARRAY], - [false, JsonInterface::TYPE_ARRAY], - [['a' => 'b'], JsonInterface::TYPE_ARRAY], - [$object, JsonInterface::TYPE_OBJECT] - ]; - } -} diff --git a/lib/internal/Magento/Framework/Serialize/README.md b/lib/internal/Magento/Framework/Serialize/README.md new file mode 100644 index 0000000000000000000000000000000000000000..e636fc79818af6f749745cde1f2d76fea0473dcf --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/README.md @@ -0,0 +1,5 @@ +# Serialize + +**Serialize** libaray provides *SerializerInterface* and multiple implementations of serializer to support different kinds of needs of serializing/unserializing of data. Here are list of serializers in this library: + + * **Json** (default) - It can be used to serialize string, integer, float, boolean, or array data to json string; it unserializes json string to string, integer, float, boolean, or array. This is the recommended serializer. \ No newline at end of file diff --git a/lib/internal/Magento/Framework/Serialize/Serializer/Json.php b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php new file mode 100644 index 0000000000000000000000000000000000000000..009fee5e3639f5ab5cd8cf9e1031f4a4129d98b4 --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Serializer/Json.php @@ -0,0 +1,27 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Serialize\Serializer; + +use Magento\Framework\Serialize\SerializerInterface; + +class Json implements SerializerInterface +{ + /** + * {@inheritDoc} + */ + public function serialize($data) + { + return json_encode($data); + } + + /** + * {@inheritDoc} + */ + public function unserialize($string) + { + return json_decode($string, true); + } +} diff --git a/lib/internal/Magento/Framework/Serialize/SerializerInterface.php b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php new file mode 100644 index 0000000000000000000000000000000000000000..1dc70da80f39455690d34e750b07b9270c2d8d5e --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/SerializerInterface.php @@ -0,0 +1,25 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Serialize; + +interface SerializerInterface +{ + /** + * Serialize data into string + * + * @param string|int|float|bool|array|null $data + * @return string|bool + */ + public function serialize($data); + + /** + * Unserialize the given string + * + * @param string $string + * @return string|int|float|bool|array|null + */ + public function unserialize($string); +} diff --git a/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php new file mode 100644 index 0000000000000000000000000000000000000000..38fa7d2a66f7f179a93bd11f45a35a996f937fe8 --- /dev/null +++ b/lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php @@ -0,0 +1,45 @@ +<?php +/** + * Copyright © 2016 Magento. All rights reserved. + * See COPYING.txt for license details. + */ +namespace Magento\Framework\Serialize\Test\Unit\Serializer; + +use Magento\Framework\Serialize\Serializer\Json; + +class JsonTest extends \PHPUnit_Framework_TestCase +{ + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + private $json; + + protected function setUp() + { + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + $this->json = $objectManager->getObject(Json::class); + } + + /** + * @param null|bool|array $value + * @dataProvider serializeUnserializeDataProvider + */ + public function testSerializeUnserialize($value) + { + $this->assertEquals( + $this->json->unserialize($this->json->serialize($value)), + $value + ); + } + + public function serializeUnserializeDataProvider() + { + return [ + [''], + ['string'], + [null], + [false], + [['a' => 'b']], + ]; + } +}