Skip to content
PHP Ecosystem
Explore

Json

API reference › yii › helpers › Json

classyii\helpers\Json
ExtendsBaseJson
Since2.0
Sourceframework/helpers/Json.php

Json is a helper class providing JSON data encoding and decoding.

It enhances the PHP built-in functions json_encode() and json_decode() by supporting encoding JavaScript expressions and throwing exceptions when decoding fails.

PropertyTypeDescriptionDefined by
$jsonErrorMessages staticarrayList of JSON Error messages assigned to constant names for better handling of PHP <= 5.5. Default: ['JSON_ERROR_SYNTAX' => 'Syntax error', 'JSON_ERROR_UNSUPPORTED_TYPE' => 'Type is not supported', 'JSON_ERROR_DEPTH' => 'The maximum stack depth has been exceeded', 'JSON_ERROR_STATE_MISMATCH' => 'Invalid or malformed JSON', 'JSON_ERROR_CTRL_CHAR' => 'Control character error, possibly incorrectly encoded', 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded']. Available since 2.0.7.BaseJson
$keepObjectType staticboolAvoids objects with zero-indexed keys to be encoded as array Json::encode((object)['test']) will be encoded as an object not as an array. Default: false. Available since 2.0.44.BaseJson
$prettyPrint staticbool|nullEnables human readable output a.k.a. Default: null. Available since 2.0.43.BaseJson
MethodDescriptionDefined by
decode() staticDecodes the given JSON string into a PHP data structure.BaseJson
encode() staticEncodes the given value into a JSON string.BaseJson
errorSummary() staticGenerates a summary of the validation errors.BaseJson
htmlEncode() staticEncodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.BaseJson
handleJsonError() protected staticHandles encode() and decode() errors by throwing exceptions with the respective error message.BaseJson
processData() protected staticPre-processes the data before sending it to json_encode().BaseJson