Merge pull request #8528 from creative-commoners/pulls/4.4/deprecate-json-methods

API Convert JSON methods are now deprecated, use json_encode or decode instead
This commit is contained in:
Loz Calver 2018-10-29 17:50:36 +01:00 committed by GitHub
commit 81561cf762
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 19 additions and 17 deletions

View File

@ -102,7 +102,7 @@ class PjaxResponseNegotiator
throw new HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400); throw new HTTPResponse_Exception("X-Pjax = '$fragment' not supported for this URL.", 400);
} }
} }
$response->setBody(Convert::raw2json($responseParts)); $response->setBody(json_encode($responseParts));
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
return $response; return $response;

View File

@ -150,6 +150,7 @@ class Convert
* Encode a value as a JSON encoded string. You can optionally pass a bitmask of * Encode a value as a JSON encoded string. You can optionally pass a bitmask of
* JSON constants as options through to the encode function. * JSON constants as options through to the encode function.
* *
* @deprecated 4.4.0:5.0.0 Use json_encode() instead
* @param mixed $val Value to be encoded * @param mixed $val Value to be encoded
* @param int $options Optional bitmask of JSON constants * @param int $options Optional bitmask of JSON constants
* @return string JSON encoded string * @return string JSON encoded string
@ -161,15 +162,15 @@ class Convert
/** /**
* Encode an array as a JSON encoded string. * Encode an array as a JSON encoded string.
* This is an alias to {@link raw2json()}
* *
* @deprecated 4.4.0:5.0.0 Use json_encode() instead
* @param array $val Array to convert * @param array $val Array to convert
* @param int $options Optional bitmask of JSON constants * @param int $options Optional bitmask of JSON constants
* @return string JSON encoded string * @return string JSON encoded string
*/ */
public static function array2json($val, $options = 0) public static function array2json($val, $options = 0)
{ {
return self::raw2json($val, $options); return json_encode($val, $options);
} }
/** /**
@ -240,6 +241,7 @@ class Convert
/** /**
* Convert a JSON encoded string into an object. * Convert a JSON encoded string into an object.
* *
* @deprecated 4.4.0:5.0.0 Use json_decode() instead
* @param string $val * @param string $val
* @return object|boolean * @return object|boolean
*/ */
@ -251,7 +253,7 @@ class Convert
/** /**
* Convert a JSON string into an array. * Convert a JSON string into an array.
* *
* @uses json2obj * @deprecated 4.4.0:5.0.0 Use json_decode() instead
* @param string $val JSON string to convert * @param string $val JSON string to convert
* @return array|boolean * @return array|boolean
*/ */

View File

@ -95,12 +95,12 @@ class VersionProvider
$cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock'); $cache = Injector::inst()->get(CacheInterface::class . '.VersionProvider_composerlock');
$cacheKey = md5($jsonData); $cacheKey = md5($jsonData);
if ($versions = $cache->get($cacheKey)) { if ($versions = $cache->get($cacheKey)) {
$lockData = Convert::json2array($versions); $lockData = json_decode($versions, true);
} }
} }
if (empty($lockData) && $jsonData) { if (empty($lockData) && $jsonData) {
$lockData = Convert::json2array($jsonData); $lockData = json_decode($jsonData, true);
if ($cache) { if ($cache) {
$cache->set($cacheKey, $jsonData); $cache->set($cacheKey, $jsonData);

View File

@ -376,7 +376,7 @@ class FormRequestHandler extends RequestHandler
$acceptType = $this->getRequest()->getHeader('Accept'); $acceptType = $this->getRequest()->getHeader('Accept');
if (strpos($acceptType, 'application/json') !== false) { if (strpos($acceptType, 'application/json') !== false) {
// Send validation errors back as JSON with a flag at the start // Send validation errors back as JSON with a flag at the start
$response = new HTTPResponse(Convert::array2json($result->getMessages())); $response = new HTTPResponse(json_encode($result->getMessages()));
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
return $response; return $response;
} }

View File

@ -262,7 +262,7 @@ class GridFieldAddExistingAutocompleter implements GridField_HTMLProvider, GridF
); );
} }
Config::unnest(); Config::unnest();
$response = new HTTPResponse(Convert::array2json($json)); $response = new HTTPResponse(json_encode($json));
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
return $response; return $response;
} }

View File

@ -291,7 +291,7 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
'clearAction' => GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->getAttribute('name') 'clearAction' => GridField_FormAction::create($gridField, 'reset', false, 'reset', null)->getAttribute('name')
]; ];
return Convert::raw2json($schema); return json_encode($schema);
} }
/** /**
@ -377,7 +377,7 @@ class GridFieldFilterHeader implements GridField_URLHandler, GridField_HTMLProvi
$data = FormSchema::singleton() $data = FormSchema::singleton()
->getMultipartSchema($parts, $schemaID, $form); ->getMultipartSchema($parts, $schemaID, $form);
$response = new HTTPResponse(Convert::raw2json($data)); $response = new HTTPResponse(json_encode($data));
$response->addHeader('Content-Type', 'application/json'); $response->addHeader('Content-Type', 'application/json');
return $response; return $response;
} }

View File

@ -48,7 +48,7 @@ class GridField_ActionMenu implements GridField_ColumnProvider, GridField_Action
} }
$templateData = ArrayData::create([ $templateData = ArrayData::create([
'Schema' => Convert::raw2json($schema), 'Schema' => json_encode($schema),
]); ]);
$template = SSViewer::get_templates_by_class($this, '', static::class); $template = SSViewer::get_templates_by_class($this, '', static::class);

View File

@ -371,7 +371,7 @@ class TinyMCEConfig extends HTMLEditorConfig
{ {
return [ return [
'data-editor' => 'tinyMCE', // Register ss.editorWrappers.tinyMCE 'data-editor' => 'tinyMCE', // Register ss.editorWrappers.tinyMCE
'data-config' => Convert::array2json($this->getConfig()), 'data-config' => json_encode($this->getConfig()),
]; ];
} }

View File

@ -487,7 +487,7 @@ abstract class DBField extends ViewableData implements DBIndexable
*/ */
public function JSON() public function JSON()
{ {
return Convert::raw2json($this->RAW()); return json_encode($this->RAW());
} }
/** /**

View File

@ -64,7 +64,7 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest
array((string)$btns[0]['name'] => 1) array((string)$btns[0]['name'] => 1)
); );
$this->assertFalse($response->isError()); $this->assertFalse($response->isError());
$result = Convert::json2array($response->getBody()); $result = json_decode($response->getBody(), true);
$this->assertEquals(1, count($result)); $this->assertEquals(1, count($result));
$this->assertEquals( $this->assertEquals(
array(array( array(array(
@ -81,7 +81,7 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest
array((string)$btns[0]['name'] => 1) array((string)$btns[0]['name'] => 1)
); );
$this->assertFalse($response->isError()); $this->assertFalse($response->isError());
$result = Convert::json2array($response->getBody()); $result = json_decode($response->getBody(), true);
$this->assertEquals(1, count($result), "The relational filter did not work"); $this->assertEquals(1, count($result), "The relational filter did not work");
$response = $this->post( $response = $this->post(
@ -90,7 +90,7 @@ class GridFieldAddExistingAutocompleterTest extends FunctionalTest
array((string)$btns[0]['name'] => 1) array((string)$btns[0]['name'] => 1)
); );
$this->assertFalse($response->isError()); $this->assertFalse($response->isError());
$result = Convert::json2array($response->getBody()); $result = json_decode($response->getBody(), true);
$this->assertEmpty($result, 'The output is either an empty array or boolean FALSE'); $this->assertEmpty($result, 'The output is either an empty array or boolean FALSE');
} }

View File

@ -69,7 +69,7 @@ class HTMLEditorConfigTest extends SapphireTest
) )
); );
$attributes = $c->getAttributes(); $attributes = $c->getAttributes();
$config = Convert::json2array($attributes['data-config']); $config = json_decode($attributes['data-config'], true);
$plugins = $config['external_plugins']; $plugins = $config['external_plugins'];
$this->assertNotEmpty($plugins); $this->assertNotEmpty($plugins);