Final Class Yiisoft\Request\Body\Parser\JsonParser
| Inheritance | Yiisoft\Request\Body\Parser\JsonParser |
|---|---|
| Implements | Yiisoft\Request\Body\ParserInterface |
Parses application/json requests where JSON is in the body.
Public Methods
| Method | Description | Defined By |
|---|---|---|
| __construct() | Yiisoft\Request\Body\Parser\JsonParser | |
| parse() | Yiisoft\Request\Body\Parser\JsonParser |
Method Details
| public mixed __construct ( boolean $convertToAssociativeArray = true, integer $depth = 512, integer $options = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE ) | ||
| $convertToAssociativeArray | boolean |
Whether objects should be converted to associative array during parsing. |
| $depth | integer |
Maximum JSON recursion depth. |
| $options | integer |
JSON decoding options. {@see \Yiisoft\Request\Body\Parser\json_decode()}. |
public function __construct(
private readonly bool $convertToAssociativeArray = true,
private readonly int $depth = 512,
private readonly int $options = JSON_THROW_ON_ERROR | JSON_INVALID_UTF8_IGNORE,
) {
}
| public mixed parse ( string $rawBody ) | ||
| $rawBody | string | |
public function parse(string $rawBody)
{
if ($rawBody === '') {
return null;
}
try {
$result = json_decode($rawBody, $this->convertToAssociativeArray, $this->depth, $this->options);
if (is_array($result) || is_object($result)) {
return $result;
}
} catch (JsonException $e) {
throw new ParserException('Invalid JSON data in request body: ' . $e->getMessage());
}
return null;
}
Signup or Login in order to comment.