src/Leevel/Http/Response.php
public function setData($data = [], ?int $encodingOptions = null): IResponse
    {
        if ($this->checkFlowControl()) {
            return $this;
        }

        $this->original = $data;

        if (null === $encodingOptions) {
            $encodingOptions = 256;
        }

        if ($data instanceof IArray) {
            $data = json_encode($data->toArray(), $encodingOptions);
        } elseif (is_object($data) && $data instanceof IJson) {
            $data = $data->toJson($encodingOptions);
        } elseif (is_object($data) && $data instanceof JsonSerializable) {
            $data = json_encode($data->jsonSerialize(), $encodingOptions);
        } else {
            $data = json_encode($data, $encodingOptions);
        }

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new InvalidArgumentException(json_last_error_msg());
        }

        $this->content = (string) $data;

        return $this;
    }
public function setData($data = [], ?int $encodingOptions = null): IResponse
    {
        if ($this->checkFlowControl()) {
            return $this;
        }

        $this->original = $data;

        if (null === $encodingOptions) {
            $encodingOptions = 256;
        }

        if ($data instanceof IArray) {
            $data = json_encode($data->toArray(), $encodingOptions);
        } elseif (is_object($data) && $data instanceof IJson) {
            $data = $data->toJson($encodingOptions);
        } elseif (is_object($data) && $data instanceof JsonSerializable) {
            $data = json_encode($data->jsonSerialize(), $encodingOptions);
        } else {
            $data = json_encode($data, $encodingOptions);
        }

        if (JSON_ERROR_NONE !== json_last_error()) {
            throw new InvalidArgumentException(json_last_error_msg());
        }

        $this->content = (string) $data;

        return $this;
    }
设置 JSON 数据.

@param mixed $data
@param int   $encodingOptions

@return $this