Commit 70c86491 authored by Joshua Spence's avatar Joshua Spence
Browse files

Use phutil_json_decode instead of json_decode

Summary: Generally, `phutil_json_decode` should be preferred over `json_decode`.

Test Plan: Eyellballed.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D12680
parent 05e745d9
......@@ -53,7 +53,7 @@ foreach ($sheets as $name => $sheet) {
if (!$args->getArg('force')) {
if (Filesystem::pathExists($manifest_path)) {
$data = Filesystem::readFile($manifest_path);
$data = json_decode($data, true);
$data = phutil_json_decode($data);
if (!$sheet->needsRegeneration($data)) {
continue;
}
......
......@@ -647,12 +647,15 @@ final class PhabricatorConduitAPIController
// entire param dictionary JSON encoded.
$params_json = $request->getStr('params');
if (strlen($params_json)) {
$params = json_decode($params_json, true);
if (!is_array($params)) {
throw new Exception(
"Invalid parameter information was passed to method ".
"'{$method}', could not decode JSON serialization. Data: ".
$params_json);
$params = null;
try {
$params = phutil_json_decode($params_json);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht(
"Invalid parameter information was passed to method '%s'",
$method),
$ex);
}
$metadata = idx($params, '__conduit__', array());
......
......@@ -26,13 +26,17 @@ final class ConduitSSHWorkflow extends PhabricatorSSHWorkflow {
$method = head($methodv);
$json = $this->readAllInput();
$raw_params = json_decode($json, true);
if (!is_array($raw_params)) {
throw new Exception('Invalid JSON input.');
$raw_params = null;
try {
$raw_params = phutil_json_decode($json);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Invalid JSON input.'),
$ex);
}
$params = idx($raw_params, 'params', '[]');
$params = json_decode($params, true);
$params = phutil_json_decode($params);
$metadata = idx($params, '__conduit__', array());
unset($params['__conduit__']);
......
......@@ -33,9 +33,9 @@ final class DarkConsoleDataController extends PhabricatorController {
return new Aphront400Response();
}
$result = json_decode($result, true);
if (!is_array($result)) {
try {
$result = phutil_json_decode($result);
} catch (PhutilJSONParserException $ex) {
return new Aphront400Response();
}
......
......@@ -17,9 +17,12 @@ final class DifferentialParseRenderTestCase extends PhabricatorTestCase {
$opt_file = $dir.$file.'.options';
if (Filesystem::pathExists($opt_file)) {
$options = Filesystem::readFile($opt_file);
$options = json_decode($options, true);
if (!is_array($options)) {
throw new Exception("Invalid options file: {$opt_file}.");
try {
$options = phutil_json_decode($options);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Invalid options file: %s.', $opt_file),
$ex);
}
} else {
$options = array();
......
......@@ -86,7 +86,7 @@ final class DifferentialSetDiffPropertyConduitAPIMethod
protected function execute(ConduitAPIRequest $request) {
$diff_id = $request->getValue('diff_id');
$name = $request->getValue('name');
$data = json_decode($request->getValue('data'), true);
$data = phutil_json_decode($request->getValue('data'));
self::updateDiffProperty($diff_id, $name, $data);
......
......@@ -154,9 +154,11 @@ final class DiffusionLintSaveRunner {
$files);
foreach (new LinesOfALargeExecFuture($future) as $json) {
$paths = json_decode($json, true);
if (!is_array($paths)) {
fprintf(STDERR, "Invalid JSON: {$json}\n");
$paths = null;
try {
$paths = phutil_json_decode($json);
} catch (PhutilJSONParserException $ex) {
fprintf(STDERR, pht("Invalid JSON: %s\n", $json));
continue;
}
......
......@@ -11,8 +11,9 @@ final class DiffusionLastModifiedController extends DiffusionController {
$viewer = $request->getUser();
$paths = $request->getStr('paths');
$paths = json_decode($paths, true);
if (!is_array($paths)) {
try {
$paths = phutil_json_decode($paths);
} catch (PhutilJSONParserException $ex) {
return new Aphront400Response();
}
......
......@@ -9,7 +9,7 @@ final class DiffusionMercurialWireSSHTestCase extends PhabricatorTestCase {
$raw = Filesystem::readFile($data.$file);
$raw = explode("\n~~~~~~~~~~\n", $raw, 2);
$this->assertEqual(2, count($raw));
$expect = json_decode($raw[1], true);
$expect = phutil_json_decode($raw[1]);
$this->assertTrue(is_array($expect), $file);
$this->assertParserResult($expect, $raw[0], $file);
......
......@@ -7,8 +7,9 @@ final class DoorkeeperTagsController extends PhabricatorController {
$viewer = $request->getUser();
$tags = $request->getStr('tags');
$tags = json_decode($tags, true);
if (!is_array($tags)) {
try {
$tags = phutil_json_decode($tags);
} catch (PhutilJSONParserException $ex) {
$tags = array();
}
......
......@@ -12,8 +12,9 @@ final class PhabricatorHelpKeyboardShortcutController
$user = $request->getUser();
$keys = $request->getStr('keys');
$keys = json_decode($keys, true);
if (!is_array($keys)) {
try {
$keys = phutil_json_decode($keys);
} catch (PhutilJSONParserException $ex) {
return new Aphront400Response();
}
......
......@@ -633,14 +633,16 @@ abstract class HeraldAdapter {
// dictionary. The first regexp must match the dictionary key, and the
// second regexp must match the dictionary value. If any key/value pair
// in the dictionary matches both regexps, the condition is satisfied.
$regexp_pair = json_decode($condition_value, true);
if (!is_array($regexp_pair)) {
$regexp_pair = null;
try {
$regexp_pair = phutil_json_decode($condition_value);
} catch (PhutilJSONParserException $ex) {
throw new HeraldInvalidConditionException(
'Regular expression pair is not valid JSON!');
pht('Regular expression pair is not valid JSON!'));
}
if (count($regexp_pair) != 2) {
throw new HeraldInvalidConditionException(
'Regular expression pair is not a pair!');
pht('Regular expression pair is not a pair!'));
}
$key_regexp = array_shift($regexp_pair);
......@@ -705,8 +707,10 @@ abstract class HeraldAdapter {
}
break;
case self::CONDITION_REGEXP_PAIR:
$json = json_decode($condition_value, true);
if (!is_array($json)) {
$json = null;
try {
$json = phutil_json_decode($condition_value);
} catch (PhutilJSONParserException $ex) {
throw new HeraldInvalidConditionException(
pht(
'The regular expression pair "%s" is not valid JSON. Enter a '.
......
......@@ -258,7 +258,15 @@ final class HeraldRuleController extends HeraldController {
$errors[] = pht('Rule must have a name.');
}
$data = json_decode($request->getStr('rule'), true);
$data = null;
try {
$data = phutil_json_decode($request->getStr('rule'));
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to decode rule data.'),
$ex);
}
if (!is_array($data) ||
!$data['conditions'] ||
!$data['actions']) {
......
......@@ -47,7 +47,7 @@ final class ManiphestBatchEditController extends ManiphestController {
$actions = $request->getStr('actions');
if ($actions) {
$actions = json_decode($actions, true);
$actions = phutil_json_decode($actions);
}
if ($request->isFormPost() && is_array($actions)) {
......
......@@ -74,7 +74,7 @@ final class ManiphestTransactionPreviewController extends ManiphestController {
break;
case PhabricatorTransactions::TYPE_EDGE:
if ($value) {
$value = json_decode($value);
$value = phutil_json_decode($value);
}
if (!$value) {
$value = array();
......
......@@ -120,9 +120,13 @@ final class PhabricatorMailImplementationMailgunAdapter
list($body) = $future->resolvex();
$response = json_decode($body, true);
if (!is_array($response)) {
throw new Exception("Failed to JSON decode response: {$body}");
$response = null;
try {
$response = phutil_json_decode($body);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode response.'),
$ex);
}
if (!idx($response, 'id')) {
......
......@@ -78,8 +78,10 @@ final class PhabricatorMailImplementationSendGridAdapter
if (!$user || !$key) {
throw new Exception(
"Configure 'sendgrid.api-user' and 'sendgrid.api-key' to use ".
"SendGrid for mail delivery.");
pht(
"Configure '%s' and '%s' to use SendGrid for mail delivery.",
'sendgrid.api-user',
'sendgrid.api-key'));
}
$params = array();
......@@ -142,14 +144,18 @@ final class PhabricatorMailImplementationSendGridAdapter
list($body) = $future->resolvex();
$response = json_decode($body, true);
if (!is_array($response)) {
throw new Exception("Failed to JSON decode response: {$body}");
$response = null;
try {
$response = phutil_json_decode($body);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht('Failed to JSON decode response.'),
$ex);
}
if ($response['message'] !== 'success') {
$errors = implode(';', $response['errors']);
throw new Exception("Request failed with errors: {$errors}.");
throw new Exception(pht('Request failed with errors: %s.', $errors));
}
return true;
......
......@@ -32,7 +32,7 @@ final class PhabricatorContentSource {
}
public static function newFromSerialized($serialized) {
$dict = json_decode($serialized, true);
$dict = phutil_json_decode($serialized);
if (!is_array($dict)) {
$dict = array();
}
......
......@@ -93,15 +93,18 @@ final class PhameSkinSpecification {
}
private static function loadSkinSpecification($path) {
$config_path = $path.DIRECTORY_SEPARATOR.'skin.json';
$config = array();
if (Filesystem::pathExists($config_path)) {
$config = Filesystem::readFile($config_path);
$config = json_decode($config, true);
if (!is_array($config)) {
throw new Exception(
"Skin configuration file '{$config_path}' is not a valid JSON file.");
try {
$config = phutil_json_decode($config);
} catch (PhutilJSONParserException $ex) {
throw new PhutilProxyException(
pht(
"Skin configuration file '%s' is not a valid JSON file.",
$config_path),
$ex);
}
$type = idx($config, 'type', self::TYPE_BASIC);
} else {
......
......@@ -94,20 +94,23 @@ final class PhortunePaymentMethodCreateController
if (!$errors) {
$client_token_raw = $request->getStr('token');
$client_token = json_decode($client_token_raw, true);
if (!is_array($client_token)) {
$client_token = null;
try {
$client_token = phutil_json_decode($client_token_raw);
} catch (PhutilJSONParserException $ex) {
$errors[] = pht(
'There was an error decoding token information submitted by the '.
'client. Expected a JSON-encoded token dictionary, received: %s.',
nonempty($client_token_raw, pht('nothing')));
} else {
if (!$provider->validateCreatePaymentMethodToken($client_token)) {
$errors[] = pht(
'There was an error with the payment token submitted by the '.
'client. Expected a valid dictionary, received: %s.',
$client_token_raw);
}
}
if (!$provider->validateCreatePaymentMethodToken($client_token)) {
$errors[] = pht(
'There was an error with the payment token submitted by the '.
'client. Expected a valid dictionary, received: %s.',
$client_token_raw);
}
if (!$errors) {
$errors = $provider->createPaymentMethodFromRequest(
$request,
......@@ -215,8 +218,10 @@ final class PhortunePaymentMethodCreateController
$errors = array();
$client_errors = json_decode($client_errors_raw, true);
if (!is_array($client_errors)) {
$client_errors = null;
try {
$client_errors = phutil_json_decode($client_errors_raw);
} catch (PhutilJSONParserException $ex) {
$errors[] = pht(
'There was an error decoding error information submitted by the '.
'client. Expected a JSON-encoded list of error codes, received: %s.',
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment