Skip to content

Commit

Permalink
better json encoding options
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangut committed Oct 18, 2018
1 parent 331b79e commit e827355
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"prefer-stable": true,
"require": {
"php": "^7.0",
"ext-json": "*",
"juliangut/http-exception": "^0.1.1",
"psr/log": "^1.0",
"slim/slim": "^3.5",
Expand Down
17 changes: 15 additions & 2 deletions src/Whoops/Formatter/Json.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,20 @@ class Json extends JsonResponseHandler implements ExceptionFormatter
{
use FormatterTrait;

/**
* JSON encoding options.
* Preserve float values and encode &, ', ", < and > characters in the resulting JSON.
*/
const JSON_ENCODE_OPTIONS = \JSON_UNESCAPED_UNICODE
| \JSON_UNESCAPED_SLASHES
| \JSON_PRESERVE_ZERO_FRACTION
| \JSON_HEX_AMP
| \JSON_HEX_APOS
| \JSON_HEX_QUOT
| \JSON_HEX_TAG
| \JSON_PARTIAL_OUTPUT_ON_ERROR
| \JSON_PRETTY_PRINT;

/**
* JsonHandler constructor.
*/
Expand Down Expand Up @@ -61,9 +75,8 @@ public function handle(): int
$addTrace = $this->addTraceToOutput();

$error = $this->getExceptionData($inspector, $addTrace);
$options = JSON_PARTIAL_OUTPUT_ON_ERROR | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT;

echo \json_encode(['error' => $error], $options);
echo \json_encode(['error' => $error], static::JSON_ENCODE_OPTIONS);

return Handler::QUIT;
}
Expand Down

0 comments on commit e827355

Please sign in to comment.