ENHANCEMENT Convert JSON functions now use the Services_JSON library where appropriate instead of custom code, and if json_decode() or json_encode() are available these are used

MINOR Added unit tests for Convert JSON functions


git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/branches/2.4@100423 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Sean Harvey 2010-03-03 23:05:39 +00:00 committed by Sam Minnee
parent 149496dda4
commit 4e2272334a
2 changed files with 55 additions and 23 deletions

View File

@ -105,7 +105,7 @@ class Convert extends Object {
if(function_exists('json_encode')) {
return json_encode($val);
} else {
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
$json = new Services_JSON();
return $json->encode($val);
}
@ -139,6 +139,23 @@ class Convert extends Object {
}
}
/**
* Uses the PHP 5.2 native json_encode function if available,
* otherwise falls back to the Services_JSON class.
*
* @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);
}
}
/**
* Uses the PHP 5.2 native json_decode function if available,
* otherwise falls back to the Services_JSON class.
@ -149,9 +166,13 @@ class Convert extends Object {
* @return mixed JSON safe string
*/
static function json2obj($val) {
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
$json = new Services_JSON();
return $json->decode($val);
if(function_exists('json_decode')) {
return json_decode($val);
} else {
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
$json = new Services_JSON();
return $json->decode($val);
}
}
/**
@ -204,21 +225,6 @@ class Convert extends Object {
}
return (string) $xml;
}
static function array2json( $array ) {
if(function_exists("json_encode")) {
return json_encode($array);
}
$result = array();
foreach( $array as $key => $value )
if( is_array( $value ) )
$result[] = "'$key':" . Convert::array2json( $value );
else
$result[] = "'$key':'$value'";
return '{' . implode( ', ', $result ) . '}';
}
/**
* Create a link if the string is a valid URL
@ -333,6 +339,4 @@ class Convert extends Object {
);
}
}
?>
}

View File

@ -49,5 +49,33 @@ class ConvertTest extends SapphireTest {
$val2 = 'This is some normal text.';
$this->assertEquals('This is some normal text.', Convert::xml2raw($val2), 'Normal text is not escaped');
}
function testArray2JSON() {
$val = array(
'Joe' => 'Bloggs',
'Tom' => 'Jones',
'My' => array(
'Complicated' => 'Structure'
)
);
$encoded = Convert::array2json($val);
$this->assertEquals('{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}', $encoded, 'Array is encoded in JSON');
}
function testJSON2Array() {
$val = '{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}';
$decoded = Convert::json2array($val);
$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');
}
function testJSON2Obj() {
$val = '{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}';
$obj = Convert::json2obj($val);
$this->assertEquals('Bloggs', $obj->Joe);
$this->assertEquals('Jones', $obj->Tom);
$this->assertEquals('Structure', $obj->My->Complicated);
}
}