From 461269195bbf70ac54928250738504e6c1913ac7 Mon Sep 17 00:00:00 2001 From: dag Date: Tue, 29 Mar 2022 22:45:00 +0200 Subject: [PATCH] fix: ignore partial json_encode() errors in JsonFormat (#2554) Without this change, JsonFormat simply returns an empty array. #2283 --- formats/JsonFormat.php | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/formats/JsonFormat.php b/formats/JsonFormat.php index 2c5cd073..f0aa0e65 100644 --- a/formats/JsonFormat.php +++ b/formats/JsonFormat.php @@ -111,12 +111,15 @@ class JsonFormat extends FormatAbstract { } $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 - ini_set('mbstring.substitute_character', 'none'); - $toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8'); - return $toReturn; + return $json; } public function display(){