Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use built-in json functions in PHP instead of third-party Services_JSON library #281

Merged
merged 3 commits into from Mar 31, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 21 additions & 55 deletions core/Convert.php
Expand Up @@ -92,28 +92,28 @@ static function raw2js($val) {
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);
Expand All @@ -138,41 +138,15 @@ static function xml2raw($val) {
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 object|boolean
*/
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);
}

/**
Expand All @@ -183,15 +157,7 @@ static function json2obj($val) {
* @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);
}

/**
Expand Down
32 changes: 32 additions & 0 deletions docs/en/changelogs/3.0.0.md
Expand Up @@ -113,6 +113,38 @@ As with any SilverStripe upgrade, we recommend database backups before calling `
See [mysql.com](http://dev.mysql.com/doc/refman/5.5/en/converting-tables-to-innodb.html) for details on the conversion.
Note: MySQL has made InnoDB the default engine in its [5.5 release](http://dev.mysql.com/doc/refman/5.5/en/innodb-storage-engine.html).

### Convert::json2array() changes ###

Convert JSON functions have been changed to use built-in json PHP functions `json_decode()` and `json_encode()`

Because `json_decode()` will convert nested JSON structures to arrays as well, this has changed the way it worked,
as before nested structures would be converted to an object instead.

So, given the following JSON input to `Convert::json2array()`:

{"Joe":"Bloggs","Tom":"Jones","My":{"Complicated":"Structure"}}

Here's the output from SilverStripe 2.4, with nested JSON as objects:

array(
'Joe' => 'Bloggs'
'Tom' => 'Jones',
'My' => stdObject(
Complicated => 'Structure' // property on object
)
)

Now in SilverStripe 3.x, nested structures are arrays:

array(
'Joe' => 'Bloggs',
'Tom' => 'Jones',
'My' => array(
'Complicated' => 'Structure' // key value on nested array
)
)


### GridField: Replacement for TableListField and ComplexTableField ###

We have a new component for managing lists of objects: The `[GridField](/topics/grid-field)`.
Expand Down
3 changes: 2 additions & 1 deletion tests/core/ConvertTest.php
Expand Up @@ -101,6 +101,7 @@ function testJSON2Array() {
$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() {
Expand All @@ -121,4 +122,4 @@ function testRaw2URL() {
$this->assertEquals('foos-bar-2', Convert::raw2url('foo\'s [bar] (2)'));
}

}
}
Expand Up @@ -33,7 +33,7 @@ function testSearch() {
);
$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() {
Expand Down Expand Up @@ -80,4 +80,4 @@ function Form() {
$field = new GridField('testfield', 'testfield', $player->Teams(), $config);
return new Form($this, 'Form', new FieldList($field), new FieldList());
}
}
}
8 changes: 0 additions & 8 deletions thirdparty/json/.piston.yml

This file was deleted.