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
@ -94,24 +94,24 @@ class Convert {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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
|
* @param mixed $val Value to be encoded
|
||||||
* @uses Director::baseFolder()
|
* @return string JSON encoded string
|
||||||
* @uses Services_JSON
|
|
||||||
*
|
|
||||||
* @param mixed $val
|
|
||||||
* @return string JSON safe 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();
|
* Encode an array as a JSON encoded string.
|
||||||
return $json->encode($val);
|
* 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) {
|
||||||
@ -139,40 +139,14 @@ class Convert {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 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() {
|
||||||
|
@ -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() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user