From 8ae474b18252f7b5ebccc6c626adce99be8d01e7 Mon Sep 17 00:00:00 2001 From: Sean Harvey Date: Fri, 30 Mar 2012 16:18:14 +1300 Subject: [PATCH] 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. --- core/Convert.php | 76 +++++-------------- tests/core/ConvertTest.php | 3 +- .../GridFieldAddExistingAutocompleterTest.php | 4 +- 3 files changed, 25 insertions(+), 58 deletions(-) diff --git a/core/Convert.php b/core/Convert.php index ac13d72ae..61a79fbbe 100644 --- a/core/Convert.php +++ b/core/Convert.php @@ -92,28 +92,28 @@ class Convert { 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 @@ class Convert { 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 @@ 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); } /** diff --git a/tests/core/ConvertTest.php b/tests/core/ConvertTest.php index b94b79cc7..5e82eec0f 100644 --- a/tests/core/ConvertTest.php +++ b/tests/core/ConvertTest.php @@ -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() { @@ -121,4 +122,4 @@ class ConvertTest extends SapphireTest { $this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)')); } -} \ No newline at end of file +} diff --git a/tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php b/tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php index af7397865..0d0eecd30 100644 --- a/tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php +++ b/tests/forms/gridfield/GridFieldAddExistingAutocompleterTest.php @@ -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() { @@ -80,4 +80,4 @@ class GridFieldAddExistingAutocompleterTest_Controller extends Controller implem $field = new GridField('testfield', 'testfield', $player->Teams(), $config); return new Form($this, 'Form', new FieldList($field), new FieldList()); } -} \ No newline at end of file +}