0 follower

Final Class Yiisoft\Request\Body\Parser\JsonParser

InheritanceYiisoft\Request\Body\Parser\JsonParser
ImplementsYiisoft\Request\Body\ParserInterface

Parses application/json requests where JSON is in the body.

Method Details

Hide inherited methods

__construct() public method

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,
) {
}

            
parse() public method

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;
}