Final Class Yiisoft\Json\Json
| Inheritance | Yiisoft\Json\Json |
|---|
Json is a helper class providing JSON data encoding and decoding.
It enhances the PHP built-in functions json_encode() and json_decode()
by throwing exceptions when decoding fails.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| decode() | Decodes the given JSON string into a PHP data structure. | Yiisoft\Json\Json |
| encode() | Encodes the given value into a JSON string. | Yiisoft\Json\Json |
| htmlEncode() | Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. | Yiisoft\Json\Json |
Method Details
Decodes the given JSON string into a PHP data structure.
| public static mixed decode ( string $json, boolean $asArray = true, integer $depth = 512, integer $options = JSON_THROW_ON_ERROR ) | ||
| $json | string |
The JSON string to be decoded. |
| $asArray | boolean |
Whether to return objects in terms of associative arrays. |
| $depth | integer |
The recursion depth. |
| $options | integer |
The decode options. |
| return | mixed |
The PHP data. |
|---|---|---|
| throws | JsonException |
If there is any decoding error. |
public static function decode(
string $json,
bool $asArray = true,
int $depth = 512,
int $options = JSON_THROW_ON_ERROR
) {
if ($json === '') {
return null;
}
return json_decode($json, $asArray, $depth, JSON_THROW_ON_ERROR | $options);
}
Encodes the given value into a JSON string.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
| public static string encode ( mixed $value, integer $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR, integer $depth = 512 ) | ||
| $value | mixed |
The data to be encoded. |
| $options | integer |
The encoding options. For more details please refer to
{@see https://www.php.net/manual/en/function.json-encode.php}.
Default is |
| $depth | integer |
The maximum depth. |
| return | string |
The encoding result. |
|---|---|---|
| throws | JsonException |
if there is any encoding error. |
public static function encode(
$value,
int $options = JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_THROW_ON_ERROR,
int $depth = 512
): string {
if (is_array($value)) {
$value = self::processArray($value);
} elseif (is_object($value)) {
$value = self::processObject($value);
}
/**
* @var string We use flag `JSON_THROW_ON_ERROR`, so `json_encode` never returns `false`.
*/
return json_encode($value, JSON_THROW_ON_ERROR | $options, $depth);
}
Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
| public static string htmlEncode ( mixed $value ) | ||
| $value | mixed |
The data to be encoded. |
| return | string |
The encoding result. |
|---|---|---|
| throws | JsonException |
If there is any encoding error. |
public static function htmlEncode($value): string
{
return self::encode(
$value,
JSON_UNESCAPED_UNICODE | JSON_HEX_QUOT | JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_THROW_ON_ERROR
);
}
Signup or Login in order to comment.