fix: ignore partial json_encode() errors in JsonFormat (#2554)

Without this change, JsonFormat simply returns
an empty array. #2283
This commit is contained in:
dag 2022-03-29 22:45:00 +02:00 committed by GitHub
parent 060b4c7d58
commit 461269195b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 8 additions and 5 deletions

View File

@ -111,12 +111,15 @@ class JsonFormat extends FormatAbstract {
} }
$data['items'] = $items; $data['items'] = $items;
$toReturn = json_encode($data, JSON_PRETTY_PRINT); /**
* The intention here is to discard non-utf8 byte sequences.
* But the JSON_PARTIAL_OUTPUT_ON_ERROR also discards lots of other errors.
* So consider this a hack.
* Switch to JSON_INVALID_UTF8_IGNORE when PHP 7.2 is the latest platform requirement.
*/
$json = json_encode($data, JSON_PRETTY_PRINT | JSON_PARTIAL_OUTPUT_ON_ERROR);
// Remove invalid non-UTF8 characters return $json;
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
} }
public function display(){ public function display(){