Skip to content

Commit

Permalink
fix: ignore partial json_encode() errors in JsonFormat (#2554)
Browse files Browse the repository at this point in the history
Without this change, JsonFormat simply returns
an empty array. #2283
  • Loading branch information
dvikan authored Mar 29, 2022
1 parent 060b4c7 commit 4612691
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions formats/JsonFormat.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,12 +111,15 @@ public function stringify(){
}
$data['items'] = $items;

$toReturn = json_encode($data, JSON_PRETTY_PRINT);

// Remove invalid non-UTF8 characters
ini_set('mbstring.substitute_character', 'none');
$toReturn = mb_convert_encoding($toReturn, $this->getCharset(), 'UTF-8');
return $toReturn;
/**
* 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);

return $json;
}

public function display(){
Expand Down

0 comments on commit 4612691

Please sign in to comment.