mirror of
https://github.com/silverstripe/silverstripe-framework
synced 2024-10-22 12:05:37 +00:00
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:
parent
18a1cc1db3
commit
8ae474b182
@ -92,28 +92,28 @@ class Convert {
|
|||||||
return str_replace(array("\\", '"', "\n", "\r", "'"), array("\\\\", '\"', '\n', '\r', "\\'"), $val);
|
return str_replace(array("\\", '"', "\n", "\r", "'"), array("\\\\", '\"', '\n', '\r', "\\'"), $val);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Uses the PHP 5.2 native json_encode function if available,
|
* Encode a value as a JSON encoded string.
|
||||||
* 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
|
|
||||||
*
|
*
|
||||||
* @param mixed $val
|
* @param mixed $val Value to be encoded
|
||||||
* @return string JSON safe string
|
* @return string JSON encoded string
|
||||||
*/
|
*/
|
||||||
static function raw2json($val) {
|
static function raw2json($val) {
|
||||||
if(function_exists('json_encode')) {
|
return json_encode($val);
|
||||||
return json_encode($val);
|
|
||||||
} else {
|
|
||||||
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
|
|
||||||
$json = new Services_JSON();
|
|
||||||
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) {
|
static function raw2sql($val) {
|
||||||
if(is_array($val)) {
|
if(is_array($val)) {
|
||||||
foreach($val as $k => $v) $val[$k] = self::raw2sql($v);
|
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');
|
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.
|
* 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
|
* @param string $val
|
||||||
* @return mixed JSON safe string
|
* @return mixed
|
||||||
*/
|
*/
|
||||||
static function json2obj($val) {
|
static function json2obj($val) {
|
||||||
require_once(Director::baseFolder() . '/sapphire/thirdparty/json/JSON.php');
|
return json_decode($val);
|
||||||
$json = new Services_JSON();
|
|
||||||
return $json->decode($val);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -183,15 +157,7 @@ class Convert {
|
|||||||
* @return array|boolean
|
* @return array|boolean
|
||||||
*/
|
*/
|
||||||
static function json2array($val) {
|
static function json2array($val) {
|
||||||
$json = self::json2obj($val);
|
return json_decode($val, true);
|
||||||
if(!$json) return false;
|
|
||||||
|
|
||||||
$arr = array();
|
|
||||||
foreach($json as $k => $v) {
|
|
||||||
$arr[$k] = $v;
|
|
||||||
}
|
|
||||||
|
|
||||||
return $arr;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -101,6 +101,7 @@ class ConvertTest extends SapphireTest {
|
|||||||
$this->assertEquals(3, count($decoded), '3 items in the decoded array');
|
$this->assertEquals(3, count($decoded), '3 items in the decoded array');
|
||||||
$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
|
$this->assertContains('Bloggs', $decoded, 'Contains "Bloggs" value in decoded array');
|
||||||
$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
|
$this->assertContains('Jones', $decoded, 'Contains "Jones" value in decoded array');
|
||||||
|
$this->assertContains('Structure', $decoded['My']['Complicated']);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testJSON2Obj() {
|
function testJSON2Obj() {
|
||||||
@ -121,4 +122,4 @@ class ConvertTest extends SapphireTest {
|
|||||||
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
|
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest {
|
|||||||
);
|
);
|
||||||
$this->assertFalse($response->isError());
|
$this->assertFalse($response->isError());
|
||||||
$result = Convert::json2array($response->getBody());
|
$result = Convert::json2array($response->getBody());
|
||||||
$this->assertFalse($result);
|
$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testAdd() {
|
function testAdd() {
|
||||||
@ -80,4 +80,4 @@ class GridFieldAddExistingAutocompleterTest_Controller extends Controller implem
|
|||||||
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
|
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
|
||||||
return new Form($this, 'Form', new FieldList($field), new FieldList());
|
return new Form($this, 'Form', new FieldList($field), new FieldList());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user