Skip to content

Commit 61eda85

Browse files
author
Joan He
committed
MAGETWO-59444: Create serializer interface and json class in framework
1 parent 6a08cdd commit 61eda85

File tree

3 files changed

+51
-11
lines changed

3 files changed

+51
-11
lines changed

lib/internal/Magento/Framework/Serialize/Serializer/Json.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77

88
use Magento\Framework\Serialize\SerializerInterface;
99

10+
/**
11+
* Class for serializing data to json string and unserializing json string to data
12+
*
13+
*/
1014
class Json implements SerializerInterface
1115
{
1216
/**

lib/internal/Magento/Framework/Serialize/SerializerInterface.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
*/
66
namespace Magento\Framework\Serialize;
77

8+
/**
9+
* Interface for serializing
10+
*/
811
interface SerializerInterface
912
{
1013
/**

lib/internal/Magento/Framework/Serialize/Test/Unit/Serializer/JsonTest.php

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66
namespace Magento\Framework\Serialize\Test\Unit\Serializer;
77

8+
use Magento\Framework\DataObject;
89
use Magento\Framework\Serialize\Serializer\Json;
910

1011
class JsonTest extends \PHPUnit_Framework_TestCase
@@ -21,25 +22,57 @@ protected function setUp()
2122
}
2223

2324
/**
24-
* @param null|bool|array $value
25-
* @dataProvider serializeUnserializeDataProvider
25+
* @param string|int|float|bool|array|null $value
26+
* @param string $expected
27+
* @dataProvider serializeDataProvider
2628
*/
27-
public function testSerializeUnserialize($value)
29+
public function testSerialize($value, $expected)
2830
{
2931
$this->assertEquals(
30-
$this->json->unserialize($this->json->serialize($value)),
31-
$value
32+
$expected,
33+
$this->json->serialize($value)
3234
);
3335
}
3436

35-
public function serializeUnserializeDataProvider()
37+
public function serializeDataProvider()
3638
{
39+
$dataObject = new DataObject(['something']);
3740
return [
38-
[''],
39-
['string'],
40-
[null],
41-
[false],
42-
[['a' => 'b']],
41+
['', '""'],
42+
['string', '"string"'],
43+
[null, 'null'],
44+
[false, 'false'],
45+
[['a' => 'b', 'd' => 123], '{"a":"b","d":123}'],
46+
[123, '123'],
47+
[10.56, '10.56'],
48+
[$dataObject, '{}'],
49+
];
50+
}
51+
52+
/**
53+
* @param string $value
54+
* @param string|int|float|bool|array|null $expected
55+
* @dataProvider unserializeDataProvider
56+
*/
57+
public function testUnserialize($value, $expected)
58+
{
59+
$this->assertEquals(
60+
$expected,
61+
$this->json->unserialize($value)
62+
);
63+
}
64+
65+
public function unserializeDataProvider()
66+
{
67+
return [
68+
['""', ''],
69+
['"string"', 'string'],
70+
['null', null],
71+
['false', false],
72+
['{"a":"b","d":123}', ['a' => 'b', 'd' => 123]],
73+
['123', 123],
74+
['10.56', 10.56],
75+
['{}', []],
4376
];
4477
}
4578
}

0 commit comments

Comments
 (0)