Json decode an http response.

@param  \Psr\Http\Message\ResponseInterface $response
@return array
@throws \DevMob\Postcodes\Exceptions\JsonException
507223
public static function decode(ResponseInterface $response): array
    {
        $contents = $response->getBody()->getContents();
        $decoded = json_decode($contents, true);
        $response->getBody()->rewind();

        if (json_last_error() !== JSON_ERROR_NONE || ! is_array($decoded)) {
            throw new JsonException(json_last_error_msg());
        }

        return $decoded;
    }
871800
protected function _unserializeResponseBody(ResponseInterface $response) {

		$body = (string) $response->getBody();
		$contentType = $response->getHeaderLine('Content-type');

		try {
			if(false !== stripos($contentType, $this->dataType)
				&& isset($this->unserializers[$this->dataType])) {
				/** @var UnserializerInterface $unserializer */
				$unserializer = \Yii::createObject($this->unserializers[$this->dataType]);
				if($unserializer instanceof UnserializerInterface) {
					return $unserializer->unserialize($body, false);
				}
			}

			return $body;

		} catch(InvalidParamException $e) {

			return $body;
		}
	}
52276
private function isJsonResponse(ResponseInterface $response)
    {
        if ($response->getStatusCode() == 200) {
            return;
        }

        if ($response->getStatusCode() == 400) {
            throw new InvalidRequestException($response);
        }

        if ($response->getStatusCode() == 401) {
            throw new UnauthorizedRequestException($response);
        }

        throw new ServerResponseException($response);
    }
1031631
private function withError(ResponseInterface $response, $throwable)
    {
        $contents = [
            'error' => 'Application Error',
        ];

        if ($this->displayErrorDetails) {
            $throwables = $this->unpackThrowables($throwable);
            $contents = [
                'error' => $throwable->getMessage(),
                'details' => $this->formatStacktraceForExceptions($throwables),
            ];
        }

        return $this
            ->withJSON($response, $contents)
            ->withStatus(500);
    }
226590
protected function decode($response)
    {
        $lines = array_slice(explode("\n", trim($response)), 1);

        $result = [];
        foreach ($lines as $line) {
            if ($line[0] == '-') {
                $result[] = trim(ltrim($line, '- '));
            } else {
                $key = strtok($line, ': ');
                if ($key) {
                    $value = ltrim(trim(strtok('')), ' ');

                    if (is_numeric($value)) {
                        $value = $value + 0;
                    }

                    $result[$key] = $value;
                }
            }
        }

        return $result;
    }
345645
protected function parseResponseIntoArray($response)
    {
        if (!$response->isContentType('json')) {
            parse_str($response->getBody(true), $array);

            return $array;
        }

        return $response->json();
    }
697301
private function decodeResponse(ResponseInterface $response)
    {
        $responseBody = trim((string) $response->getBody());

        if ($response->getHeader('Content-Type')[0] === 'text/plain;charset=UTF-8') {
            $lines = explode("\n", $responseBody);
            $data = [];

            // parse endomondo text response
            foreach ($lines as $line) {
                $items = explode('=', $line);
                if (count($items) === 2) {
                    $data[$items[0]] = $items[1];
                }
            }

            return $data;
        }

        $data = json_decode($responseBody, true);

        if (isset($data['error'])) {
            throw new EndomondoApiOldException('Api error: ' . $data['error']['type']);
        }

        return $data;
    }
1292541
protected function decodeData(ResponseObject $response)
    {
        $resultArray = json_decode($response->getBody(), true);
        if ($resultArray === null) {
            throw new JsonException('Failed to decode response', json_last_error());
        }

        return $resultArray;
    }
29265
protected function decryptResponse(StreamInterface $response): array
    {
        parse_str($response->getContents(), $responseHeader);

        $decryptedResponseHeader = $this
            ->computopApiService
            ->decryptResponseHeader($responseHeader, $this->config->getBlowfishPass());

        return $decryptedResponseHeader;
    }
1308644
public static function prepareHeaders(ResponseInterface $response): array
    {
        $preparedHeaders = [];
        $statusHeader = rtrim(self::generateStatusLine($response), "\r\n");

        $preparedHeaders[] = $statusHeader;
        $headers = $response->getHeaders();
        if ($headers instanceof Headers) {
            $preparedHeaders = array_merge($preparedHeaders, $headers->getPreparedValues());
        } else {
            foreach (array_keys($headers) as $name) {
                $preparedHeaders[] = $response->getHeaderLine($name);
            }
        }

        return $preparedHeaders;
    }
403753
protected function convertResponse(ResponseInterface $response)
    {
        $this->response->setContent($response->getBody());
        $this->response->setStatusCode($response->getStatusCode());
        foreach ($response->getHeaders() as $name => $values) {
            $this->response->setHeader($name, implode(', ', $values));
        }

        return $this->response;
    }
631255
public static function getContent(ResponseInterface $response)
    {
        if ($response->getStatusCode() === 204) {
            return [];
        }

        $body = (string) $response->getBody();

        if (!$body) {
            return [];
        }

        if (strpos($response->getHeaderLine('Content-Type'), 'application/json') !== 0) {
            throw new DecodingFailedException('The content type header was not application/json.');
        }

        return self::jsonDecode($body);
    }
940165
private function parseMakeUpExam(ResponseInterface $response)
    {
        $body = $this->checkAndThrow($response);

        // form => 考试形式, district => 校区
        return Parser::table($body, '#DataGrid1', [
            'code', 'name', 'student_name', 'time', 'location', 'seat', 'form'
        ], 7);
    }
1255130
public function unwrapResponse(ResponseInterface $response)
    {
        if ($response->getStatusCode() != 200) {
            throw new ServerNetworkException($response->getStatusCode(), $response->getHeaders(), $response->getBody()->getContents());
        }
        $content = \GuzzleHttp\json_decode($response->getBody()->getContents(), true);
        if (isset($content['ActionStatus']) && $content['ActionStatus'] == 'OK') {
            $this->succeed = true;
            foreach ($content as $name => $value) {
                $setter = 'set' . $name;
                if (method_exists($this, $setter)) {
                    $this->$setter($value);
                } else if (property_exists($this, $name)) {
                    $this->{$name} = $value;
                } else {
                    $this->_content[$name] = $value;
                }
            }
        } else {
            $this->succeed = false;
            if (isset($content['ActionStatus']) && $content['ActionStatus'] == 'FAIL') {
                throw new ServerErrorHttpException($content['ErrorInfo'], $content['ErrorCode']);
            }
        }
    }
96649
public function header(ResponseInterface $response = null)
    {
        if (null === $response) {
            /** @var PageInterface $page */
            $page = $this['page'];
            $response = new Response($page->httpResponseCode(), $page->httpHeaders(), '');
        }

        header("HTTP/{$response->getProtocolVersion()} {$response->getStatusCode()} {$response->getReasonPhrase()}");
        foreach ($response->getHeaders() as $key => $values) {
            foreach ($values as $i => $value) {
                header($key . ': ' . $value, $i === 0);
            }
        }
    }
347435
protected function getJsonFromResponse(Response $response)
    {
        $responseBody = $response->getBody();
        if ($response->getStatusCode() >= 400) {
            throw new Exception($responseBody);
        }
        $json = @json_decode($responseBody, true);
        if ($json === null) {
            throw new Exception('Failed to decode CommunityTranslation response');
        }

        return $json;
    }
1006395
protected function getResponseData(Response $response)
    {
        if ($response instanceof JsonResponse) {
            /** @var $response JsonResponse */
            return $response->getData(true) ?: [];
        }

        $content = $response->getContent();

        return json_decode($content, true) ?: false;
    }