1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-15 10:57:08 +01:00
psalm/src/Psalm/Internal/Json/Json.php
Daniil Gentili 404db2bb1a
Fix JSON reports with long UTF8 strings (#5300)
* Fix JSON reports with long UTF8 strings

* CS fix

* UTF8-safe snippets
2021-03-01 11:23:21 -05:00

45 lines
955 B
PHP

<?php
namespace Psalm\Internal\Json;
use RuntimeException;
use function json_encode;
use function json_last_error_msg;
use const JSON_PRETTY_PRINT;
use const JSON_UNESCAPED_SLASHES;
use const JSON_UNESCAPED_UNICODE;
/**
* Provides ability of pretty printed JSON output.
*/
class Json
{
public const PRETTY = JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE;
/**
* @var int
*/
public const DEFAULT = 0;
/**
* @param mixed $data
*
*
* @psalm-pure
*/
public static function encode($data, ?int $options = null): string
{
if ($options === null) {
$options = self::DEFAULT;
}
$result = json_encode($data, $options);
if ($result === false) {
/** @psalm-suppress ImpureFunctionCall */
throw new RuntimeException('Cannot create JSON string: '.json_last_error_msg());
}
return $result;
}
}