nulib/php/src_php/json/JsonException.php

21 lines
622 B
PHP
Raw Normal View History

2023-12-31 22:37:04 +04:00
<?php
namespace nulib\php\json;
use RuntimeException;
class JsonException extends RuntimeException {
static final function json_last_error(?string $prefix=null): ?self {
$json_last_error = json_last_error();
if ($json_last_error === JSON_ERROR_NONE) return null;
$message = json_last_error_msg()." ($json_last_error)";
if ($prefix) $message = "$prefix: $message";
return new static($message);
}
static final function ensure_json_value($value, ?string $prefix=null) {
$exception = self::json_last_error($prefix);
if ($exception === null) return $value;
else throw $exception;
}
}