Skip to content
Snippets Groups Projects
Commit 363f1caa authored by Joan He's avatar Joan He
Browse files

MAGETWO-59444: Create serializer interface and json class in framework

parent 3977f578
No related merge requests found
......@@ -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\Json" />
<type name="Magento\Framework\Model\ResourceModel\Db\TransactionManager" shared="false" />
<type name="Magento\Framework\Logger\Handler\Base">
<arguments>
......
......@@ -3,23 +3,23 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Json;
namespace Magento\Framework\Serialize;
class Json implements JsonInterface
class Json implements SerializerInterface
{
/**
* {@inheritDoc}
*/
public function encode($data, $options = 0)
public function serialize($data)
{
return json_encode($data, $options);
return json_encode($data);
}
/**
* {@inheritDoc}
*/
public function decode($string, $objectDecodeType = self::TYPE_ARRAY)
public function unserialize($string)
{
return json_decode($string, $objectDecodeType);
return json_decode($string, true);
}
}
......@@ -3,36 +3,24 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Json;
namespace Magento\Framework\Serialize;
interface JsonInterface
interface SerializerInterface
{
/**
* 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
* Serialize data into string
*
* @param array|string $data
* @param int $options
* @return string|bool
*/
public function encode($data, $options = 0);
public function serialize($data);
/**
* Decode the given string which is encoded in the JSON format
* Unserialize the given string into array
*
* @param string $string
* @param int $objectDecodeType
* @return array|\stdClass
* @return array
*/
public function decode($string, $objectDecodeType = self::TYPE_ARRAY);
public function unserialize($string);
}
......@@ -3,15 +3,15 @@
* Copyright © 2016 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework\Json\Test\Unit;
namespace Magento\Framework\Serialize\Test\Unit;
use Magento\Framework\Json\JsonInterface;
use Magento\Framework\Json\Json;
use Magento\Framework\Serialize\SerializerInterface;
use Magento\Framework\Serialize\Json;
class JsonTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Json
* @var \Magento\Framework\Serialize\Json
*/
private $json;
......@@ -26,10 +26,10 @@ class JsonTest extends \PHPUnit_Framework_TestCase
* @param int $objectDecodeType
* @dataProvider encodeDecodeDataProvider
*/
public function testEncodeDecode($value, $objectDecodeType)
public function testEncodeDecode($value)
{
$this->assertEquals(
$this->json->decode($this->json->encode($value), $objectDecodeType),
$this->json->unserialize($this->json->serialize($value)),
$value
);
}
......@@ -39,11 +39,10 @@ class JsonTest extends \PHPUnit_Framework_TestCase
$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]
[''],
[null],
[false],
[['a' => 'b']],
];
}
}
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment