JSON

Sends JSON response

@param mixed $data
@param integer $status
@return string
779352
public static function json($data, $status = NULL) {
        if ($status) {
            parent::setStatus($status);
        }
        parent::setContentType('application/json');
        return json_encode($data);
    }
1353802
public static function send($message = '', $status = 200)
    {
        if (self::$response === null) {
            self::init();
        }

        self::$response->setStatusCode(intval($status));
        self::$response->setContent(strval($message));
        self::$response->send();
    }
625805
protected function renderJson($data, $callback = null, $status = 200, $headers = array()) {
		// Check if a output action has be already performed
		$this->_checkIfPerformed();

		// Create a JSON response
		$jsonResponse = \Symfony\Component\HttpFoundation\JsonResponse::create($data, $status, $headers);

		// Set callback
		if ( null !== $callback )
			$jsonResponse->setCallback($callback);

		// Add request informationen
		$jsonResponse->prepare($this->application->requestStack->getCurrentRequest());

		// Replace response object
		$this->application->response = $jsonResponse;
	}
237963
protected function response($data, $statusCode = 200, array $headers = [])
    {
        if ($data instanceof Arrayable && !$data instanceof JsonSerializable) {
            $data = $data->toArray();
        }

        return new JsonResponse($data, $statusCode, $headers);
    }
405317
public function respond(Status $status, $content = '')
    {
        $this->header($status);

        if (!empty($content)) {
            $this->send($content);
        }

        return $this;
    }
844217
public function success($data = null, $statusCode = null, array $meta = []):JsonResponse
    {
        if (is_integer($data)) {
            list($data, $statusCode, $meta) = [null, $data, is_array($statusCode) ? $statusCode : []];
        }

        if (is_array($statusCode)) {
            list($statusCode, $meta) = [200, $statusCode];
        }

        return $this->successResponse->transform($data)->addMeta($meta)->respond($statusCode);
    }
526840
public function outputJson($data = null, $message = '', $status = 200)
    {
        // 错误信息返回格式可参考：[https://developer.github.com/v3/]
        if ($status != 200 && $message !== '') {
            $data = [
                'message' => $message
            ];
        }
        parent::outputJson($data, $status);
    }
1017242
public function respondWithSuccess(array $data = []): JsonResponse
    {
        return $this->setStatusCode(IlluminateResponse::HTTP_OK)->respond([
            'status' => [
                'http_code' => $this->getStatusCode(),
            ],
            'data' => $data,
        ]);
    }
535226
public function withJson($data, int $status = null, int $options = 0)
        {
            $json = json_encode($data, $options);
            if ($json === false) {
                throw new \RuntimeException(json_last_error_msg(), json_last_error());
            }
            
            $response = $this->write($json);
            $response = $response->withHeader('Content-Type', 'application/json;charset=utf-8');
            
            if (isset($status)) {
                return $response->withStatus($status);
            }
            return $response;
        }
186946
public function raw(string $data = '', int $status = 200): Response
    {
        $response = $this;

        // Headers
        $response = $response->withoutHeader('Content-Type')->withAddedHeader('Content-Type', 'text/plain');
        $this->getCharset() && $response = $response->withCharset($this->getCharset());

        // Content
        $data && $response = $response->withContent($data);

        // Status code
        $status && $response = $response->withStatus($status);

        return $response;
    }
984981
public static function responseJson($mdata = array(), $status = null, $msg = '', array $headers = array(), $options = 0)
    {
        $response = self::response($mdata, $status, $msg);
        return Response::json($response, $response['status'], $headers, $options);
    }
545407
public function getJsonResponse($data)
    {
        $view = new JsonModel();
        $view->setTerminal(true);

        $response = $this->getResponse();
        $response->setContent(json_encode($data));

        return $response;
    }
534281
public function withJson($data, $status = null, $encodingOptions = JSON_ENCODE_OPTION)
    {
        return parent::withJson($data, $status, $encodingOptions);
    }
1279172
public function outputJson($data = null, $status = 200)
    {
        $this->getContext()->getOutput()->outputJson($data, $status);
    }
234118
public function resourceJson($data = array(), $status = 200, array $headers = array())
  {
    $headers = array_merge($headers, $this->getRequestLimitHeader());
    $headers = array_merge($headers, $this->getConfig('headers'));

    return $this->getResponse()->resourceJson($data, $status, $headers);
  }
560483
public function respond(array $data, $status = 200, $code, array $headers = [], $options = 0)
    {
        return $this->response->json(
            array_merge(compact('status', 'code'), $data), $status, $headers, $options
        );
    }
847766
protected function json($data, int $status = 200, array $headers = [], array $context = []): JsonResponse
    {
        if ($this->container->has('serializer')) {
            $json = $this->container->get('serializer')->serialize($data, 'json', array_merge([
                'json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS,
            ], $context));

            return new JsonResponse($json, $status, $headers, true);
        }

        return new JsonResponse($data, $status, $headers);
    }