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 (from r100423)

git-svn-id: svn://svn.silverstripe.com/silverstripe/open/modules/sapphire/trunk@105567 467b73ca-7a2a-4603-9d3b-597d59a354a9
This commit is contained in:
Ingo Schommer 2010-05-25 03:52:38 +00:00
parent a5608657cc
commit 30b2a950fe
2 changed files with 55 additions and 23 deletions

View File

@ -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,10 +166,14 @@ class Convert extends Object {
* @return mixed JSON safe string
*/
static function json2obj($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);
}
}
/**
* Convert a JSON string into an array.
@ -205,21 +226,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
* @param string The string to linkify
@ -334,5 +340,3 @@ class Convert extends Object {
}
}
?>

View File

@ -50,4 +50,32 @@ class ConvertTest extends SapphireTest {
$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);
}
}