如何在 Apigility ApiProblem 响应上启用 Json 漂亮的打印?
How to enable Json pretty print on Apigility ApiProblem response?
我想知道如何将 ApiProblem 响应设置为默认漂亮打印 Json。
在 ZF-Hal 中有一个 HalJsonStrategy
用于渲染。本策略extends the default ZF2 JsonStrategy
. This strategy renders view models of type HalJsonModel
which extends默认ZF2JsonModel
class。
使用 the HalJsonRenderer
which extends 默认 ZF2 JsonRenderer
class.
进行渲染
您可以通过将渲染策略更改为某种自定义策略来自定义所有这些,或者通过在 HalJsonStrategy
.
中设置另一个渲染器(扩展现有渲染器的自定义渲染器)来自定义所有这些
不确定什么是最好的方法。
由于所有这些 hal-json 渲染逻辑都建立在默认 ZF2 json 渲染逻辑之上,因此很可能只需更改即可更改当前渲染器输出 json 的方式与您通常在 ZF2 中进行的配置一样,以获得漂亮的打印输出。
也许此页面 here 可以帮助您实现您的目标。
总结:
归结为将 the render
method in the JsonRenderer
class 中的 Json::encode
调用(或渲染调用)交换为 Json::prettyPrint
注:
这样做的原因可能是为了在浏览器 window 中以可读的方式检查您的 json 代码。有许多 json 插件可以帮助您解决这个问题,这将是一个更简单的解决方案。
我对 class ApiProblemResponse
进行了一些更改。我将 属性 $jsonFlags
设置为 128,这与 JSON_PRETTY_PRINT
代码有关。
这是包含我的更改的完整 class 代码:
<?php
namespace Zend\ApiProblem;
use Zend\Http\Response;
/**
* Represents an ApiProblem response payload
*/
class ApiProblemResponse extends Response
{
/**
* @var ApiProblem
*/
protected $apiProblem;
/**
* Flags to use with json_encode JSON_PRETTY_PRINT
*
* @var int
*/
protected $jsonFlags = 128;
/**
* @param ApiProblem $apiProblem
*/
public function __construct(ApiProblem $apiProblem)
{
$this->apiProblem = $apiProblem;
$this->setCustomStatusCode($apiProblem->status);
$this->setReasonPhrase($apiProblem->title);
// Just comment/remove these lines to prevent flags from being overwrited
//if (defined('JSON_UNESCAPED_SLASHES')) {
//$this->jsonFlags = constant('JSON_UNESCAPED_SLASHES');
//}
}
/**
* @return ApiProblem
*/
public function getApiProblem()
{
return $this->apiProblem;
}
/**
* Retrieve the content
*
* Serializes the composed ApiProblem instance to JSON.
*
* @return string
*/
public function getContent()
{
return json_encode($this->apiProblem->toArray(), $this->jsonFlags);
}
/**
* Retrieve headers
*
* Proxies to parent class, but then checks if we have an content-type
* header; if not, sets it, with a value of "application/problem+json".
*
* @return \Zend\Http\Headers
*/
public function getHeaders()
{
$headers = parent::getHeaders();
if (!$headers->has('content-type')) {
$headers->addHeaderLine('content-type', 'application/problem+json');
}
return $headers;
}
/**
* Override reason phrase handling
*
* If no corresponding reason phrase is available for the current status
* code, return "Unknown Error".
*
* @return string
*/
public function getReasonPhrase()
{
if (! empty($this->reasonPhrase)) {
return $this->reasonPhrase;
}
if (isset($this->recommendedReasonPhrases[$this->statusCode])) {
return $this->recommendedReasonPhrases[$this->statusCode];
}
return 'Unknown Error';
}
}
在我的控制器中,我使用了:
return new ApiProblemResponse(
new ApiProblem(ApiProblemResponse::STATUS_CODE_501, 'Example of JSON_PRETTY_PRINT response.')
);
成功了:
{
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
"title": "Not Implemented",
"status": 501,
"detail": "Example of JSON_PRETTY_PRINT response."
}
我想知道如何将 ApiProblem 响应设置为默认漂亮打印 Json。
在 ZF-Hal 中有一个 HalJsonStrategy
用于渲染。本策略extends the default ZF2 JsonStrategy
. This strategy renders view models of type HalJsonModel
which extends默认ZF2JsonModel
class。
使用 the HalJsonRenderer
which extends 默认 ZF2 JsonRenderer
class.
您可以通过将渲染策略更改为某种自定义策略来自定义所有这些,或者通过在 HalJsonStrategy
.
不确定什么是最好的方法。
由于所有这些 hal-json 渲染逻辑都建立在默认 ZF2 json 渲染逻辑之上,因此很可能只需更改即可更改当前渲染器输出 json 的方式与您通常在 ZF2 中进行的配置一样,以获得漂亮的打印输出。
也许此页面 here 可以帮助您实现您的目标。
总结:
归结为将 the render
method in the JsonRenderer
class 中的 Json::encode
调用(或渲染调用)交换为 Json::prettyPrint
注: 这样做的原因可能是为了在浏览器 window 中以可读的方式检查您的 json 代码。有许多 json 插件可以帮助您解决这个问题,这将是一个更简单的解决方案。
我对 class ApiProblemResponse
进行了一些更改。我将 属性 $jsonFlags
设置为 128,这与 JSON_PRETTY_PRINT
代码有关。
这是包含我的更改的完整 class 代码:
<?php
namespace Zend\ApiProblem;
use Zend\Http\Response;
/**
* Represents an ApiProblem response payload
*/
class ApiProblemResponse extends Response
{
/**
* @var ApiProblem
*/
protected $apiProblem;
/**
* Flags to use with json_encode JSON_PRETTY_PRINT
*
* @var int
*/
protected $jsonFlags = 128;
/**
* @param ApiProblem $apiProblem
*/
public function __construct(ApiProblem $apiProblem)
{
$this->apiProblem = $apiProblem;
$this->setCustomStatusCode($apiProblem->status);
$this->setReasonPhrase($apiProblem->title);
// Just comment/remove these lines to prevent flags from being overwrited
//if (defined('JSON_UNESCAPED_SLASHES')) {
//$this->jsonFlags = constant('JSON_UNESCAPED_SLASHES');
//}
}
/**
* @return ApiProblem
*/
public function getApiProblem()
{
return $this->apiProblem;
}
/**
* Retrieve the content
*
* Serializes the composed ApiProblem instance to JSON.
*
* @return string
*/
public function getContent()
{
return json_encode($this->apiProblem->toArray(), $this->jsonFlags);
}
/**
* Retrieve headers
*
* Proxies to parent class, but then checks if we have an content-type
* header; if not, sets it, with a value of "application/problem+json".
*
* @return \Zend\Http\Headers
*/
public function getHeaders()
{
$headers = parent::getHeaders();
if (!$headers->has('content-type')) {
$headers->addHeaderLine('content-type', 'application/problem+json');
}
return $headers;
}
/**
* Override reason phrase handling
*
* If no corresponding reason phrase is available for the current status
* code, return "Unknown Error".
*
* @return string
*/
public function getReasonPhrase()
{
if (! empty($this->reasonPhrase)) {
return $this->reasonPhrase;
}
if (isset($this->recommendedReasonPhrases[$this->statusCode])) {
return $this->recommendedReasonPhrases[$this->statusCode];
}
return 'Unknown Error';
}
}
在我的控制器中,我使用了:
return new ApiProblemResponse(
new ApiProblem(ApiProblemResponse::STATUS_CODE_501, 'Example of JSON_PRETTY_PRINT response.')
);
成功了:
{
"type": "http:\/\/www.w3.org\/Protocols\/rfc2616\/rfc2616-sec10.html",
"title": "Not Implemented",
"status": 501,
"detail": "Example of JSON_PRETTY_PRINT response."
}