JsonResponseFormatter
API reference › yii › web › JsonResponseFormatter
JsonResponseFormatter formats the given data into a JSON or JSONP response content.
It is used by Response to format response data.
To configure properties like $encodeOptions or $prettyPrint, you can configure the response
application component like the following:
'response' => [ // ... 'formatters' => [ \yii\web\Response::FORMAT_JSON => [ 'class' => 'yii\web\JsonResponseFormatter', 'prettyPrint' => YII_DEBUG, // use "pretty" output in debug mode 'keepObjectType' => false, // keep object type for zero-indexed objects // ... ], ],],Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$behaviors read-only | array<yii\<static>> | List of behaviors attached to this component. | yii\ |
$contentType | string| | Custom value of the Content-Type header of the response. Default: null. Available since 2.0.14. | JsonResponseFormatter |
$encodeOptions | int | The encoding options passed to Json::encode(). Default: 320. Available since 2.0.7. | JsonResponseFormatter |
$keepObjectType | bool | Avoids objects with zero-indexed keys to be encoded as array Json::encode((object)[‘test’]) will be encoded as an object not array. Default: null. Available since 2.0.44. | JsonResponseFormatter |
$prettyPrint | bool | Whether to format the output in a readable “pretty” format. Default: false. Available since 2.0.7. | JsonResponseFormatter |
$useJsonp | bool | Whether to use JSONP response format. Default: false. | JsonResponseFormatter |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | yii\ |
__call() | Calls the named method which is not a class method. | yii\ |
__clone() | This method is called after the object is created by cloning an existing one. | yii\ |
__get() | Returns the value of a component property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets the value of a component property. | yii\ |
__unset() | Sets a component property to be null. | yii\ |
attachBehavior() | Attaches a behavior to this component. | yii\ |
attachBehaviors() | Attaches a list of behaviors to the component. | yii\ |
behaviors() | Returns a list of behaviors that this component should behave as. | yii\ |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
detachBehavior() | Detaches a behavior from the component. | yii\ |
detachBehaviors() | Detaches all behaviors from the component. | yii\ |
ensureBehaviors() | Makes sure that the behaviors declared in behaviors() are attached to this component. | yii\ |
format() | Formats the specified response. | JsonResponseFormatter |
getBehavior() | Returns the named behavior object. | yii\ |
getBehaviors() | Returns all behaviors attached to this component. | yii\ |
hasEventHandlers() | Returns a value indicating whether there is any handler attached to the named event. | yii\ |
hasMethod() | Returns a value indicating whether a method is defined. | yii\ |
hasProperty() | Returns a value indicating whether a property is defined for this component. | yii\ |
init() | Initializes the object. | yii\ |
off() | Detaches an existing event handler from this component. | yii\ |
on() | Attaches an event handler to an event. | yii\ |
trigger() | Triggers an event. | yii\ |
formatJson() protected | Formats response data in JSON format. | JsonResponseFormatter |
formatJsonp() protected | Formats response data in JSONP format. | JsonResponseFormatter |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
CONTENT_TYPE_JSONP | 'application/javascript; charset=UTF-8' | JSON Content Type Available since 2.0.14. | JsonResponseFormatter |
CONTENT_TYPE_JSON | 'application/json; charset=UTF-8' | JSONP Content Type Available since 2.0.14. | JsonResponseFormatter |
CONTENT_TYPE_HAL_JSON | 'application/hal+json; charset=UTF-8' | HAL JSON Content Type Available since 2.0.14. | JsonResponseFormatter |
Property details
Section titled “Property details”$contentType
Section titled “$contentType ”public string|null $contentType = nullCustom value of the Content-Type header of the response.
When equals null default content type will be used based on the useJsonp property.
Since
2.0.14
$encodeOptions
Section titled “$encodeOptions ”public int $encodeOptions = 320The encoding options passed to Json::encode().
For more details please refer to
https://www.php.net/manual/en/function.json-encode.php.
Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE.
This property has no effect, when $useJsonp is true.
Since
2.0.7
$keepObjectType
Section titled “$keepObjectType ”public bool $keepObjectType = nullAvoids objects with zero-indexed keys to be encoded as array Json::encode((object)[‘test’]) will be encoded as an object not array.
This matches the behaviour of json_encode(). Defaults to Json::$keepObjectType value
Since
2.0.44
$prettyPrint
Section titled “$prettyPrint ”public bool $prettyPrint = falseWhether to format the output in a readable “pretty” format.
This can be useful for debugging purpose.
If this is true, JSON_PRETTY_PRINT will be added to $encodeOptions.
Defaults to false.
This property has no effect, when $useJsonp is true.
Since
2.0.7
$useJsonp
Section titled “$useJsonp ”public bool $useJsonp = falseWhether to use JSONP response format.
When this is true, the response data
must be an array consisting of data and callback members. The latter should be a JavaScript
function name while the former will be passed to this function as a parameter.
Method details
Section titled “Method details”format()
Section titled “format() ”public function format(yii\web\Response $response): mixedFormats the specified response.
formatJson()
Section titled “formatJson() ”protected function formatJson(yii\web\Response $response): mixedFormats response data in JSON format.
formatJsonp()
Section titled “formatJsonp() ”protected function formatJsonp(yii\web\Response $response): mixedFormats response data in JSONP format.