/
ArrayToJSONSerializer.php
49 lines (46 loc) · 1.13 KB
/
ArrayToJSONSerializer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
<?php
namespace Elasticsearch\Serializers;
/**
* Class JSONSerializer
*
* @category Elasticsearch
* @package Elasticsearch\Serializers\JSONSerializer
* @author Zachary Tong <zach@elastic.co>
* @license http://www.apache.org/licenses/LICENSE-2.0 Apache2
* @link http://elastic.co
*/
class ArrayToJSONSerializer implements SerializerInterface
{
/**
* Serialize assoc array into JSON string
*
* @param string|array $data Assoc array to encode into JSON
*
* @return string
*/
public function serialize($data)
{
if (is_string($data) === true) {
return $data;
} else {
$data = json_encode($data, JSON_PRESERVE_ZERO_FRACTION);
if ($data === '[]') {
return '{}';
} else {
return $data;
}
}
}
/**
* Deserialize JSON into an assoc array
*
* @param string $data JSON encoded string
* @param array $headers Response Headers
*
* @return array
*/
public function deserialize($data, $headers)
{
return json_decode($data, true);
}
}