From 4e2272334a1b9233f8baa4e20d14ba06a53d0381 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Wed, 3 Mar 2010 23:05:39 +0000 Subject: [PATCH] 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 --- core/Convert.php | 48 +++++++++++++++++++++++-------------------- tests/ConvertTest.php | 30 ++++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 23 deletions(-) diff --git a/core/Convert.php b/core/Convert.php index d11bdf919..cae12d7f4 100755 --- a/core/Convert.php +++ b/core/Convert.php @@ -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 { ); } -} - -?> +} \ No newline at end of file diff --git a/tests/ConvertTest.php b/tests/ConvertTest.php index dc43d66f7..e4e0e5c44 100644 --- a/tests/ConvertTest.php +++ b/tests/ConvertTest.php @@ -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); + } + } \ No newline at end of file