diff --git a/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php b/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php index 12f39ff67ec8b..488217f53ebab 100644 --- a/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php +++ b/app/code/Magento/CustomerImportExport/Test/Unit/Model/ResourceModel/Import/CustomerComposite/DataTest.php @@ -100,16 +100,19 @@ public function testGetNextBunch($entityType, $bunchData, $expectedData) $resource = $dependencies['resource']; $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $jsonDecoderMock = $this->getMockBuilder(\Magento\Framework\Json\DecoderInterface::class) - ->disableOriginalConstructor() + $serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) ->getMock(); - $jsonDecoderMock->expects($this->once()) - ->method('decode') - ->willReturn(json_decode($bunchData, true)); + $serializerMock->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($serializedData) { + return json_decode($serializedData, true); + } + ); $jsonHelper = $helper->getObject( \Magento\Framework\Json\Helper\Data::class, [ - 'jsonDecoder' => $jsonDecoderMock, + 'serializer' => $serializerMock, ] ); unset($dependencies['resource'], $dependencies['json_helper']); diff --git a/lib/internal/Magento/Framework/Json/Decoder.php b/lib/internal/Magento/Framework/Json/Decoder.php index 87f51daba35a4..5232330de5d9c 100644 --- a/lib/internal/Magento/Framework/Json/Decoder.php +++ b/lib/internal/Magento/Framework/Json/Decoder.php @@ -10,14 +10,30 @@ */ class Decoder implements DecoderInterface { + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + private $serializer; + + /** + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException + */ + public function __construct(\Magento\Framework\Serialize\Serializer\Json $serializer = null) + { + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); + } + /** * Decodes the given $data string which is encoded in the JSON format. * * @param string $data - * @return mixed + * @return array|bool|float|int|mixed|null|string + * @throws \InvalidArgumentException */ public function decode($data) { - return \Zend_Json::decode($data); + return $this->serializer->unserialize($data); } } diff --git a/lib/internal/Magento/Framework/Json/Encoder.php b/lib/internal/Magento/Framework/Json/Encoder.php index d04445bbdacb6..515647431b132 100644 --- a/lib/internal/Magento/Framework/Json/Encoder.php +++ b/lib/internal/Magento/Framework/Json/Encoder.php @@ -17,23 +17,35 @@ class Encoder implements EncoderInterface */ protected $translateInline; + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + private $serializer; + /** * @param \Magento\Framework\Translate\InlineInterface $translateInline + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException */ - public function __construct(\Magento\Framework\Translate\InlineInterface $translateInline) - { + public function __construct( + \Magento\Framework\Translate\InlineInterface $translateInline, + \Magento\Framework\Serialize\Serializer\Json $serializer = null + ) { $this->translateInline = $translateInline; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); } /** * Encode the mixed $data into the JSON format. * * @param mixed $data - * @return string + * @return bool|string + * @throws \InvalidArgumentException */ public function encode($data) { $this->translateInline->processResponseBody($data); - return \Zend_Json::encode($data); + return $this->serializer->serialize($data); } } diff --git a/lib/internal/Magento/Framework/Json/Helper/Data.php b/lib/internal/Magento/Framework/Json/Helper/Data.php index a516d65e98989..f12124ea5f5af 100644 --- a/lib/internal/Magento/Framework/Json/Helper/Data.php +++ b/lib/internal/Magento/Framework/Json/Helper/Data.php @@ -14,38 +14,51 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper { /** * @var \Magento\Framework\Json\DecoderInterface + * @deprecated */ protected $jsonDecoder; /** * @var \Magento\Framework\Json\EncoderInterface + * @deprecated */ protected $jsonEncoder; + /** + * @var \Magento\Framework\Serialize\Serializer\Json + */ + private $serializer; + /** * @param \Magento\Framework\App\Helper\Context $context * @param \Magento\Framework\Json\DecoderInterface $jsonDecoder * @param \Magento\Framework\Json\EncoderInterface $jsonEncoder + * @param \Magento\Framework\Serialize\Serializer\Json|null $serializer + * @throws \RuntimeException */ public function __construct( \Magento\Framework\App\Helper\Context $context, \Magento\Framework\Json\DecoderInterface $jsonDecoder, - \Magento\Framework\Json\EncoderInterface $jsonEncoder + \Magento\Framework\Json\EncoderInterface $jsonEncoder, + \Magento\Framework\Serialize\Serializer\Json $serializer = null ) { parent::__construct($context); $this->jsonDecoder = $jsonDecoder; $this->jsonEncoder = $jsonEncoder; + $this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance() + ->get(\Magento\Framework\Serialize\Serializer\Json::class); } /** * Encode the mixed $valueToEncode into the JSON format * * @param mixed $valueToEncode - * @return string + * @return bool|string + * @throws \InvalidArgumentException */ public function jsonEncode($valueToEncode) { - return $this->jsonEncoder->encode($valueToEncode); + return $this->serializer->serialize($valueToEncode); } /** @@ -53,10 +66,11 @@ public function jsonEncode($valueToEncode) * encoded in the JSON format * * @param string $encodedValue - * @return mixed + * @return array|bool|float|int|mixed|null|string + * @throws \InvalidArgumentException */ public function jsonDecode($encodedValue) { - return $this->jsonDecoder->decode($encodedValue); + return $this->serializer->unserialize($encodedValue); } } diff --git a/lib/internal/Magento/Framework/Json/Test/Unit/Helper/DataTest.php b/lib/internal/Magento/Framework/Json/Test/Unit/Helper/DataTest.php index 1c480302d45bd..599f5a2e318b6 100644 --- a/lib/internal/Magento/Framework/Json/Test/Unit/Helper/DataTest.php +++ b/lib/internal/Magento/Framework/Json/Test/Unit/Helper/DataTest.php @@ -18,6 +18,11 @@ class DataTest extends \PHPUnit_Framework_TestCase /** @var \Magento\Framework\Json\DecoderInterface | \PHPUnit_Framework_MockObject_MockObject */ protected $jsonDecoderMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|\Magento\Framework\Serialize\Serializer\Json + */ + private $serializerMock; + protected function setUp() { $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); @@ -27,32 +32,80 @@ protected function setUp() $this->jsonDecoderMock = $this->getMockBuilder(\Magento\Framework\Json\DecoderInterface::class) ->disableOriginalConstructor() ->getMock(); + + $this->serializerMock = $this->getMockBuilder(\Magento\Framework\Serialize\Serializer\Json::class) + ->getMock(); + $this->serializerMock->expects($this->any()) + ->method('unserialize') + ->willReturnCallback( + function ($serializedData) { + return json_decode($serializedData, true); + } + ); + $this->serializerMock->expects($this->any()) + ->method('serialize') + ->willReturnCallback( + function ($serializedData) { + return json_encode($serializedData); + } + ); $this->helper = $objectManager->getObject( \Magento\Framework\Json\Helper\Data::class, [ 'jsonEncoder' => $this->jsonEncoderMock, 'jsonDecoder' => $this->jsonDecoderMock, + 'serializer' => $this->serializerMock ] ); } - public function testJsonEncode() + /** + * @param string $value + * @param string|int|float|bool|array|null $expected + * @throws \InvalidArgumentException + * @dataProvider getJsonEncodeDataProvider + */ + public function testJsonEncode($value, $expected) + { + $this->assertEquals($expected, $this->helper->jsonEncode($value)); + } + + public function getJsonEncodeDataProvider() + { + return [ + ['', '""'], + ['string', '"string"'], + [null, 'null'], + [false, 'false'], + [['a' => 'b', 'd' => 123], '{"a":"b","d":123}'], + [123, '123'], + [10.56, '10.56'], + [new \stdClass(), '{}'], + ]; + } + + /** + * @param string $value + * @param string|int|float|bool|array|null $expected + * @throws \InvalidArgumentException + * @dataProvider getJsonDecodeDataProvider + */ + public function testJsonDecode($value, $expected) { - $expected = '"valueToEncode"'; - $valueToEncode = 'valueToEncode'; - $this->jsonEncoderMock->expects($this->once()) - ->method('encode') - ->willReturn($expected); - $this->assertEquals($expected, $this->helper->jsonEncode($valueToEncode)); + $this->assertEquals($expected, $this->helper->jsonDecode($value)); } - public function testJsonDecode() + public function getJsonDecodeDataProvider() { - $expected = '"valueToDecode"'; - $valueToDecode = 'valueToDecode'; - $this->jsonDecoderMock->expects($this->once()) - ->method('decode') - ->willReturn($expected); - $this->assertEquals($expected, $this->helper->jsonDecode($valueToDecode)); + return [ + ['""', ''], + ['"string"', 'string'], + ['null', null], + ['false', false], + ['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]], + ['123', 123], + ['10.56', 10.56], + ['{}', []], + ]; } }