Skip to content
This repository
Browse code

API CHANGE Remove use of Services_JSON and replace with json_encode()…

… and json_decode()

API CHANGE Convert::json2array() will convert nested JSON structures to array as well for easier traversal, instead of array with nested objects.
  • Loading branch information...
commit 8ae474b18252f7b5ebccc6c626adce99be8d01e7 1 parent 18a1cc1
Sean Harvey authored March 30, 2012 halkyon committed March 31, 2012
76  core/Convert.php
@@ -92,28 +92,28 @@ static function raw2js($val) {
92 92
 			return str_replace(array("\\", '"', "\n", "\r", "'"), array("\\\\", '\"', '\n', '\r', "\\'"), $val);
93 93
 		}
94 94
 	}
95  
-	
  95
+
96 96
 	/**
97  
-	 * Uses the PHP 5.2 native json_encode function if available,
98  
-	 * otherwise falls back to the Services_JSON class.
99  
-	 * 
100  
-	 * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
101  
-	 * @uses Director::baseFolder()
102  
-	 * @uses Services_JSON
  97
+	 * Encode a value as a JSON encoded string.
103 98
 	 *
104  
-	 * @param mixed $val
105  
-	 * @return string JSON safe string
  99
+	 * @param mixed $val Value to be encoded
  100
+	 * @return string JSON encoded string
106 101
 	 */
107 102
 	static function raw2json($val) {
108  
-		if(function_exists('json_encode')) {
109  
-			return json_encode($val);	
110  
-		} else {
111  
-			require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
112  
-			$json = new Services_JSON();
113  
-			return $json->encode($val);
114  
-		}
  103
+		return json_encode($val);
115 104
 	}
116  
-	
  105
+
  106
+	/**
  107
+	 * Encode an array as a JSON encoded string.
  108
+	 * THis is an alias to {@link raw2json()}
  109
+	 *
  110
+	 * @param array $val Array to convert
  111
+	 * @return string JSON encoded string
  112
+	 */
  113
+	static function array2json($val) {
  114
+		return self::raw2json($val);
  115
+	}
  116
+
117 117
 	static function raw2sql($val) {
118 118
 		if(is_array($val)) {
119 119
 			foreach($val as $k => $v) $val[$k] = self::raw2sql($v);
@@ -138,41 +138,15 @@ static function xml2raw($val) {
138 138
 			else return html_entity_decode($val, ENT_QUOTES, 'UTF-8');
139 139
 		}
140 140
 	}
141  
-	
142  
-	/**
143  
-	 * Convert an array into a JSON encoded string.
144  
-	 * 
145  
-	 * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
146  
-	 * @uses Director::baseFolder()
147  
-	 * @uses Services_JSON
148  
-	 * 
149  
-	 * @param array $val Array to convert
150  
-	 * @return string JSON encoded string
151  
-	 */
152  
-	static function array2json($val) {
153  
-		if(function_exists('json_encode')) {
154  
-			return json_encode($val);
155  
-		} else {
156  
-			require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
157  
-			$json = new Services_JSON();
158  
-			return $json->encode($val);
159  
-		}
160  
-	}
161  
-	
  141
+
162 142
 	/**
163 143
 	 * Convert a JSON encoded string into an object.
164  
-	 * 
165  
-	 * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
166  
-	 * @uses Director::baseFolder()
167  
-	 * @uses Services_JSON
168 144
 	 *
169 145
 	 * @param string $val
170  
-	 * @return mixed JSON safe string
  146
+	 * @return mixed
171 147
 	 */
172 148
 	static function json2obj($val) {
173  
-		require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
174  
-		$json = new Services_JSON();
175  
-		return $json->decode($val);
  149
+		return json_decode($val);
176 150
 	}
177 151
 
178 152
 	/**
@@ -183,15 +157,7 @@ static function json2obj($val) {
183 157
 	 * @return array|boolean
184 158
 	 */
185 159
 	static function json2array($val) {
186  
-		$json = self::json2obj($val);
187  
-		if(!$json) return false;
188  
-		
189  
-		$arr = array();
190  
-		foreach($json as $k => $v) {
191  
-			$arr[$k] = $v;
192  
-		}
193  
-		
194  
-		return $arr;
  160
+		return json_decode($val, true);
195 161
 	}
196 162
 	
197 163
 	/**
3  tests/core/ConvertTest.php
@@ -101,6 +101,7 @@ function testJSON2Array() {
101 101
 		$this->assertEquals(3, count($decoded), '3 items in the decoded array');
102 102
 		$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
103 103
 		$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
  104
+		$this->assertContains('Structure', $decoded['My']['Complicated']);
104 105
 	}
105 106
 
106 107
 	function testJSON2Obj() {
@@ -121,4 +122,4 @@ function testRaw2URL() {
121 122
 		$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
122 123
 	}
123 124
 
124  
-}
  125
+}
4  tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php
@@ -33,7 +33,7 @@ function testSearch() {
33 33
 		);
34 34
 		$this->assertFalse($response->isError());
35 35
 		$result = Convert::json2array($response->getBody());
36  
-		$this->assertFalse($result);
  36
+		$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
37 37
 	}
38 38
 
39 39
 	function testAdd() {
@@ -80,4 +80,4 @@ function Form() {
80 80
 		$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
81 81
 		return new Form($this, 'Form', new FieldList($field), new FieldList());
82 82
 	}
83  
-}
  83
+}

0 notes on commit 8ae474b

Please sign in to comment.
Something went wrong with that request. Please try again.