Permalink
Browse files

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...
1 parent 18a1cc1 commit 8ae474b18252f7b5ebccc6c626adce99be8d01e7 @halkyon halkyon committed with halkyon Mar 30, 2012
Showing with 25 additions and 58 deletions.
  1. +21 −55 core/Convert.php
  2. +2 −1 tests/core/ConvertTest.php
  3. +2 −2 tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php
View
@@ -92,28 +92,28 @@ static function raw2js($val) {
return str_replace(array("\\", '"', "\n", "\r", "'"), array("\\\\", '\"', '\n', '\r', "\\'"), $val);
}
}
-
+
/**
- * Uses the PHP 5.2 native json_encode function if available,
- * otherwise falls back to the Services_JSON class.
- *
- * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
- * @uses Director::baseFolder()
- * @uses Services_JSON
+ * Encode a value as a JSON encoded string.
*
- * @param mixed $val
- * @return string JSON safe string
+ * @param mixed $val Value to be encoded
+ * @return string JSON encoded string
*/
static function raw2json($val) {
- if(function_exists('json_encode')) {
- return json_encode($val);
- } else {
- require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
- $json = new Services_JSON();
- return $json->encode($val);
- }
+ return json_encode($val);
}
-
+
+ /**
+ * Encode an array as a JSON encoded string.
+ * THis is an alias to {@link raw2json()}
+ *
+ * @param array $val Array to convert
+ * @return string JSON encoded string
+ */
+ static function array2json($val) {
+ return self::raw2json($val);
+ }
+
static function raw2sql($val) {
if(is_array($val)) {
foreach($val as $k => $v) $val[$k] = self::raw2sql($v);
@@ -138,41 +138,15 @@ static function xml2raw($val) {
else return html_entity_decode($val, ENT_QUOTES, 'UTF-8');
}
}
-
- /**
- * Convert an array into a JSON encoded string.
- *
- * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
- * @uses Director::baseFolder()
- * @uses Services_JSON
- *
- * @param array $val Array to convert
- * @return string JSON encoded string
- */
- static function array2json($val) {
- if(function_exists('json_encode')) {
- return json_encode($val);
- } else {
- require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
- $json = new Services_JSON();
- return $json->encode($val);
- }
- }
-
+
/**
* Convert a JSON encoded string into an object.
- *
- * @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
- * @uses Director::baseFolder()
- * @uses Services_JSON
*
* @param string $val
- * @return mixed JSON safe string
+ * @return mixed
*/
static function json2obj($val) {
- require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
- $json = new Services_JSON();
- return $json->decode($val);
+ return json_decode($val);
}
/**
@@ -183,15 +157,7 @@ static function json2obj($val) {
* @return array|boolean
*/
static function json2array($val) {
- $json = self::json2obj($val);
- if(!$json) return false;
-
- $arr = array();
- foreach($json as $k => $v) {
- $arr[$k] = $v;
- }
-
- return $arr;
+ return json_decode($val, true);
}
/**
@@ -101,6 +101,7 @@ function testJSON2Array() {
$this->assertEquals(3, count($decoded), '3 items in the decoded array');
$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
+ $this->assertContains('Structure', $decoded['My']['Complicated']);
}
function testJSON2Obj() {
@@ -121,4 +122,4 @@ function testRaw2URL() {
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
}
-}
+}
@@ -33,7 +33,7 @@ function testSearch() {
);
$this->assertFalse($response->isError());
$result = Convert::json2array($response->getBody());
- $this->assertFalse($result);
+ $this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
}
function testAdd() {
@@ -80,4 +80,4 @@ function Form() {
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
return new Form($this, 'Form', new FieldList($field), new FieldList());
}
-}
+}

0 comments on commit 8ae474b

Please sign in to comment.