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.
This commit is contained in:
Sean Harvey 2012-03-30 16:18:14 +13:00 committed by Sean Harvey
parent 18a1cc1db3
commit 8ae474b182
3 changed files with 25 additions and 58 deletions

View File

@ -94,24 +94,24 @@ class Convert {
}
/**
* Uses the PHP 5.2 native json_encode function if available,
* otherwise falls back to the Services_JSON class.
* Encode a value as a JSON encoded string.
*
* @see http://pear.php.net/pepr/pepr-proposal-show.php?id=198
* @uses Director::baseFolder()
* @uses Services_JSON
*
* @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) {
@ -139,40 +139,14 @@ class Convert {
}
}
/**
* 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 @@ class Convert {
* @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);
}
/**

View File

@ -101,6 +101,7 @@ class ConvertTest extends SapphireTest {
$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() {

View File

@ -33,7 +33,7 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest {
);
$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() {