Add logging and default value when JSON encode->decode fails in JsonLD::compact

- Address part of https://github.com/friendica/friendica/issues/12011#issuecomment-1357768936
This commit is contained in:
Hypolite Petovan 2022-12-19 10:22:05 -05:00
parent 06ea61f0ed
commit f2188835e7

View File

@ -140,7 +140,7 @@ class JsonLD
* @return array Compacted JSON array * @return array Compacted JSON array
* @throws Exception * @throws Exception
*/ */
public static function compact($json, bool $logfailed = true) public static function compact($json, bool $logfailed = true): array
{ {
jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader'); jsonld_set_document_loader('Friendica\Util\JsonLD::documentLoader');
@ -203,6 +203,11 @@ class JsonLD
$json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true); $json = json_decode(json_encode($compacted, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE), true);
if ($json === false) {
Logger::notice('JSON encode->decode failed', ['orig_json' => $orig_json, 'compacted' => $compacted]);
$json = [];
}
return $json; return $json;
} }