Skip to content

Remove zend json from framework json classes #10367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -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']);
Expand Down
20 changes: 18 additions & 2 deletions lib/internal/Magento/Framework/Json/Decoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
20 changes: 16 additions & 4 deletions lib/internal/Magento/Framework/Json/Encoder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
24 changes: 19 additions & 5 deletions lib/internal/Magento/Framework/Json/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,49 +14,63 @@ 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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like these parameters may be deprecated now.

\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);
}

/**
* Decodes the given $encodedValue string which is
* 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);
}
}
81 changes: 67 additions & 14 deletions lib/internal/Magento/Framework/Json/Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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],
['{}', []],
];
}
}