yii\web\JsonResponseFormatter::formatJson PHP Method

formatJson() protected method

Formats response data in JSON format.
protected formatJson ( Response $response )
$response Response
    protected function formatJson($response)
    {
        $response->getHeaders()->set('Content-Type', 'application/json; charset=UTF-8');
        if ($response->data !== null) {
            $options = $this->encodeOptions;
            if ($this->prettyPrint) {
                $options |= JSON_PRETTY_PRINT;
            }
            $response->content = Json::encode($response->data, $options);
        }
    }

Usage Example

 protected function formatJson($response)
 {
     parent::formatJson($response);
     $response->getHeaders()->set('Content-Type', $this->getContentType());
 }