serialisation.php
3.5 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
if (!class_exists('Services_JSON', false)) {
require_once dirname(__FILE__).'/services_json.php';
}
function CS_REST_SERIALISATION_get_available($log) {
$log->log_message('Getting serialiser', __FUNCTION__, CS_REST_LOG_VERBOSE);
if(function_exists('json_decode') && function_exists('json_encode')) {
return new CS_REST_NativeJsonSerialiser($log);
} else {
return new CS_REST_ServicesJsonSerialiser($log);
}
}
class CS_REST_BaseSerialiser {
var $_log;
function __construct($log) {
$this->_log = $log;
}
/**
* Recursively ensures that all data values are utf-8 encoded.
* @param array $data All values of this array are checked for utf-8 encoding.
*/
function check_encoding($data) {
foreach($data as $k => $v) {
// If the element is a sub-array then recusively encode the array
if(is_array($v)) {
$data[$k] = $this->check_encoding($v);
// Otherwise if the element is a string then we need to check the encoding
} else if(is_string($v)) {
if((function_exists('mb_detect_encoding') && mb_detect_encoding($v) !== 'UTF-8') ||
(function_exists('mb_check_encoding') && !mb_check_encoding($v, 'UTF-8'))) {
// The string is using some other encoding, make sure we utf-8 encode
$v = utf8_encode($v);
}
$data[$k] = $v;
}
}
return $data;
}
}
class CS_REST_DoNothingSerialiser extends CS_REST_BaseSerialiser {
function get_type() { return 'do_nothing'; }
function serialise($data) { return $data; }
function deserialise($text) {
$data = json_decode($text);
return is_null($data) ? $text : $data;
}
function check_encoding($data) { return $data; }
}
class CS_REST_NativeJsonSerialiser extends CS_REST_BaseSerialiser {
function get_format() {
return 'json';
}
function get_type() {
return 'native';
}
function serialise($data) {
if(is_null($data) || $data == '') return '';
return json_encode($this->check_encoding($data));
}
function deserialise($text) {
$data = json_decode($text);
return $this->strip_surrounding_quotes(is_null($data) ? $text : $data);
}
/**
* We've had sporadic reports of people getting ID's from create routes with the surrounding quotes present.
* There is no case where these should be present. Just get rid of it.
*/
function strip_surrounding_quotes($data) {
if(is_string($data)) {
return trim($data, '"');
}
return $data;
}
}
class CS_REST_ServicesJsonSerialiser extends CS_REST_BaseSerialiser {
var $_serialiser;
function __construct($log) {
parent::__construct($log);
$this->_serialiser = new Services_JSON();
}
function get_content_type() {
return 'application/json';
}
function get_format() {
return 'json';
}
function get_type() {
return 'services_json';
}
function serialise($data) {
if(is_null($data) || $data == '') return '';
return $this->_serialiser->encode($this->check_encoding($data));
}
function deserialise($text) {
$data = $this->_serialiser->decode($text);
return is_null($data) ? $text : $data;
}
}