Skip to content

Commit 005ec16

Browse files
committed
MAGETWO-80178: [2.2.x] - Remove zend json from data object #10306
1 parent ad118e6 commit 005ec16

File tree

3 files changed

+63
-4
lines changed

3 files changed

+63
-4
lines changed

lib/internal/Magento/Framework/DataObject.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -325,19 +325,21 @@ public function convertToXml(
325325
* Convert object data to JSON
326326
*
327327
* @param array $keys array of required keys
328-
* @return string
328+
* @return bool|string
329+
* @throws \InvalidArgumentException
329330
*/
330331
public function toJson(array $keys = [])
331332
{
332333
$data = $this->toArray($keys);
333-
return \Zend_Json::encode($data);
334+
return \Magento\Framework\Serialize\JsonConverter::convert($data);
334335
}
335336

336337
/**
337338
* The "__" style wrapper for toJson
338339
*
339-
* @param array $keys
340-
* @return string
340+
* @param array $keys
341+
* @return bool|string
342+
* @throws \InvalidArgumentException
341343
*/
342344
public function convertToJson(array $keys = [])
343345
{
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Serialize;
7+
8+
/**
9+
* This class was introduced only for usage in the \Magento\Framework\DataObject::toJson method.
10+
* It should not be used in other cases and instead \Magento\Framework\Serialize\Serializer\Json::serialize
11+
* should be used.
12+
*/
13+
class JsonConverter
14+
{
15+
/**
16+
* This method should only be used by \Magento\Framework\DataObject::toJson
17+
* All other cases should use \Magento\Framework\Serialize\Serializer\Json::serialize directly
18+
*
19+
* @param string|int|float|bool|array|null $data
20+
* @return bool|string
21+
* @throws \InvalidArgumentException
22+
*/
23+
public static function convert($data)
24+
{
25+
$serializer = new \Magento\Framework\Serialize\Serializer\Json();
26+
return $serializer->serialize($data);
27+
}
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\Framework\Serialize\Test\Unit\Serializer;
8+
9+
class JsonConverterTest extends \PHPUnit\Framework\TestCase
10+
{
11+
public function testConvert()
12+
{
13+
$data = [
14+
'key' => 'value'
15+
];
16+
17+
$this->assertEquals(json_encode($data), \Magento\Framework\Serialize\JsonConverter::convert($data));
18+
}
19+
20+
/**
21+
* @expectedException \InvalidArgumentException
22+
* @expectedExceptionMessage Unable to serialize value.
23+
*/
24+
public function testConvertWithException()
25+
{
26+
//verify that exception will be thrown with invalid UTF8 sequence
27+
\Magento\Framework\Serialize\JsonConverter::convert("\xB1\x31");
28+
}
29+
}

0 commit comments

Comments
 (0)