mirror of https://github.com/MISP/MISP
chg: [internal] Cleanup code for JSON decoding
parent
0b775c77a2
commit
446cf12799
|
@ -93,7 +93,7 @@ class GalaxyElementsController extends AppController
|
|||
{
|
||||
$cluster = $this->GalaxyElement->GalaxyCluster->fetchIfAuthorized($this->Auth->user(), $clusterId, array('edit'), true, false);
|
||||
if ($this->request->is('post') || $this->request->is('put')) {
|
||||
$json = $this->GalaxyElement->jsonDecode($this->request->data['GalaxyElement']['jsonData']);
|
||||
$json = $this->_jsonDecode($this->request->data['GalaxyElement']['jsonData']);
|
||||
$flattened = Hash::flatten($json);
|
||||
$newElements = [];
|
||||
foreach ($flattened as $k => $v) {
|
||||
|
|
|
@ -65,14 +65,15 @@ class FileAccessTool
|
|||
|
||||
/**
|
||||
* @param string $file
|
||||
* @param bool $mustBeArray If true, exception will be thrown if deserialized data are not array type
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function readJsonFromFile($file)
|
||||
public static function readJsonFromFile($file, $mustBeArray = false)
|
||||
{
|
||||
$content = self::readFromFile($file);
|
||||
try {
|
||||
return JsonTool::decode($content);
|
||||
return $mustBeArray ? JsonTool::decodeArray($content) : JsonTool::decode($content);
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Could not decode JSON from file `$file`", 0, $e);
|
||||
}
|
||||
|
|
|
@ -23,25 +23,39 @@ class JsonTool
|
|||
* @param string $value
|
||||
* @returns mixed
|
||||
* @throws JsonException
|
||||
* @throws UnexpectedValueException
|
||||
*/
|
||||
public static function decode($value)
|
||||
{
|
||||
if (function_exists('simdjson_decode')) {
|
||||
// Use faster version of json_decode from simdjson PHP extension if this extension is installed
|
||||
try {
|
||||
return simdjson_decode($value, true);
|
||||
} catch (SimdJsonException $e) {
|
||||
throw new JsonException($e->getMessage(), $e->getCode(), $e);
|
||||
}
|
||||
}
|
||||
|
||||
if (defined('JSON_THROW_ON_ERROR')) {
|
||||
} elseif (defined('JSON_THROW_ON_ERROR')) {
|
||||
// JSON_THROW_ON_ERROR is supported since PHP 7.3
|
||||
return json_decode($value, true, 512, JSON_THROW_ON_ERROR);
|
||||
} else {
|
||||
$decoded = json_decode($value, true);
|
||||
if ($decoded === null) {
|
||||
throw new UnexpectedValueException('Could not parse JSON: ' . json_last_error_msg(), json_last_error());
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
$decoded = json_decode($value, true);
|
||||
if ($decoded === null) {
|
||||
throw new UnexpectedValueException('Could not parse JSON: ' . json_last_error_msg(), json_last_error());
|
||||
/**
|
||||
* @param string $value
|
||||
* @return array
|
||||
* @throws JsonException
|
||||
*/
|
||||
public static function decodeArray($value)
|
||||
{
|
||||
$decoded = self::decode($value);
|
||||
if (!is_array($decoded)) {
|
||||
throw new UnexpectedValueException('JSON must be array type, get ' . gettype($decoded));
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
|
|
|
@ -3536,14 +3536,11 @@ class AppModel extends Model
|
|||
* @return array
|
||||
* @throws JsonException
|
||||
* @throws UnexpectedValueException
|
||||
* @deprecated
|
||||
*/
|
||||
public function jsonDecode($json)
|
||||
protected function jsonDecode($json)
|
||||
{
|
||||
$decoded = JsonTool::decode($json);
|
||||
if (!is_array($decoded)) {
|
||||
throw new UnexpectedValueException('JSON must be array type, get ' . gettype($decoded));
|
||||
}
|
||||
return $decoded;
|
||||
return JsonTool::decodeArray($json);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -165,7 +165,7 @@ class AuditLog extends AppModel
|
|||
return 'Compressed';
|
||||
}
|
||||
}
|
||||
return $this->jsonDecode($change);
|
||||
return JsonTool::decode($change);
|
||||
}
|
||||
|
||||
public function beforeValidate($options = array())
|
||||
|
|
|
@ -228,7 +228,7 @@ class Feed extends AppModel
|
|||
$data = $this->feedGetUri($feed, $manifestUrl, $HttpSocket);
|
||||
|
||||
try {
|
||||
return $this->jsonDecode($data);
|
||||
return JsonTool::decodeArray($data);
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Could not parse '$manifestUrl' manifest JSON", 0, $e);
|
||||
}
|
||||
|
@ -272,7 +272,7 @@ class Feed extends AppModel
|
|||
if ($data === false) {
|
||||
return $this->feedGetUriRemote($feed, $manifestUrl, $HttpSocket)->json(); // cache file is not readable, fetch without etag
|
||||
}
|
||||
return $this->jsonDecode($data);
|
||||
return JsonTool::decodeArray($data);
|
||||
} else {
|
||||
throw $e;
|
||||
}
|
||||
|
@ -1982,7 +1982,7 @@ class Feed extends AppModel
|
|||
$data = $this->feedGetUri($feed, $path, $HttpSocket);
|
||||
|
||||
try {
|
||||
return $this->jsonDecode($data);
|
||||
return JsonTool::decodeArray($data);
|
||||
} catch (Exception $e) {
|
||||
throw new Exception("Could not parse event JSON with UUID '$eventUuid' from feed", 0, $e);
|
||||
}
|
||||
|
|
|
@ -24,18 +24,11 @@ class ObjectRelationship extends AppModel
|
|||
),
|
||||
);
|
||||
|
||||
|
||||
public function beforeValidate($options = array())
|
||||
{
|
||||
parent::beforeValidate();
|
||||
return true;
|
||||
}
|
||||
|
||||
public function afterFind($results, $primary = false)
|
||||
{
|
||||
foreach ($results as $k => $result) {
|
||||
if (!empty($results[$k]['ObjectRelationship']['format'])) {
|
||||
$results[$k]['ObjectRelationship']['format'] = json_decode($results[$k]['ObjectRelationship']['format'], true);
|
||||
if (!empty($result['ObjectRelationship']['format'])) {
|
||||
$results[$k]['ObjectRelationship']['format'] = JsonTool::decode($result['ObjectRelationship']['format'], true);
|
||||
}
|
||||
}
|
||||
return $results;
|
||||
|
@ -45,8 +38,7 @@ class ObjectRelationship extends AppModel
|
|||
{
|
||||
$relationsFile = APP . 'files/misp-objects/relationships/definition.json';
|
||||
if (file_exists($relationsFile)) {
|
||||
$file = new File($relationsFile);
|
||||
$relations = $this->jsonDecode($file->read());
|
||||
$relations = FileAccessTool::readJsonFromFile($relationsFile, true);
|
||||
if (!isset($relations['version'])) {
|
||||
$relations['version'] = 1;
|
||||
}
|
||||
|
|
|
@ -3951,7 +3951,7 @@ class Server extends AppModel
|
|||
// pass
|
||||
}
|
||||
if (!empty($execResult)) {
|
||||
$execResult = $this->jsonDecode($execResult);
|
||||
$execResult = JsonTool::decodeArray($execResult);
|
||||
$results['cli']['phpversion'] = $execResult['phpversion'];
|
||||
foreach ($execResult['extensions'] as $extension => $loaded) {
|
||||
$results['extensions'][$extension]['cli_version'] = $loaded;
|
||||
|
|
Loading…
Reference in New Issue