BaseJson
API reference › yii › helpers › BaseJson
1 subclass
BaseJson provides concrete implementation for Json.
Do not use BaseJson. Use Json instead.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$jsonErrorMessages static | array | List of JSON Error messages assigned to constant names for better handling of PHP <= 5.5. Default: ['JSON_ERROR_SYNTAX' => 'Syntax error',. Available since 2.0.7. | BaseJson |
$keepObjectType static | bool | Avoids 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 static | bool| | Enables human readable output a.k.a. Default: null. Available since 2.0.43. | BaseJson |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
decode() static | Decodes the given JSON string into a PHP data structure. | BaseJson |
encode() static | Encodes the given value into a JSON string. | BaseJson |
errorSummary() static | Generates a summary of the validation errors. | BaseJson |
htmlEncode() static | Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. | BaseJson |
handleJsonError() protected static | Handles encode() and decode() errors by throwing exceptions with the respective error message. | BaseJson |
processData() protected static | Pre-processes the data before sending it to json_encode(). | BaseJson |
Property details
Section titled “Property details”$keepObjectType
Section titled “$keepObjectType ”public static bool $keepObjectType = falseAvoids objects with zero-indexed keys to be encoded as array Json::encode((object)['test']) will be encoded as an object not as an array.
This matches the behaviour of json_encode().
Defaults to false to avoid any backwards compatibility issues.
Enable for single purpose: Json::$keepObjectType = true;
Since
2.0.44
$prettyPrint
Section titled “$prettyPrint ”public static bool|null $prettyPrint = nullEnables human readable output a.k.a.
Pretty Print.
This can useful for debugging during development but is not recommended in a production environment!
In case prettyPrint is null (default) the options passed to encode functions will not be changed.
Since
2.0.43
Method details
Section titled “Method details”decode()
Section titled “decode() ”public static function decode(string $json, bool $asArray = true): mixedDecodes the given JSON string into a PHP data structure.
Parameters
| Parameter | Type | Description |
|---|---|---|
$json | string | The JSON string to be decoded |
$asArray | bool | Whether to return objects in terms of associative arrays. |
Returns
mixed: The PHP data
Throws
yii\base\InvalidArgumentException: if there is any decoding error
encode()
Section titled “encode() ”public static function encode(mixed $value, int $options = 320): stringEncodes the given value into a JSON string.
The method enhances json_encode() by supporting JavaScript expressions.
In particular, the method will not encode a JavaScript expression that is
represented in terms of a JsExpression object.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The data to be encoded. |
$options | int | The encoding options. For more details please refer to https://www.php.net/manual/en/function.json-encode.php. Default is JSON_UNESCAPED_SLASHES |. |
Returns
string: The encoding result.
Throws
yii\base\InvalidArgumentException: if there is any encoding error.
errorSummary()
Section titled “errorSummary() ”public static function errorSummary(yii\base\Model|yii\base\Model[] $models, array $options = []): stringGenerates a summary of the validation errors.
Parameters
| Parameter | Type | Description |
|---|---|---|
$models | yii\|yii\[] | The model(s) whose validation errors are to be displayed. |
$options | array | The tag options in terms of name-value pairs. The following options are specially handled: - showAllErrors: boolean, if set to true every error message for each attribute will be shown otherwise only the first error message for each attribute will be shown. Defaults to false. |
Returns
string: The generated error summary
Since
2.0.14
htmlEncode()
Section titled “htmlEncode() ”public static function htmlEncode(mixed $value): stringEncodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code.
The method enhances json_encode() by supporting JavaScript expressions.
In particular, the method will not encode a JavaScript expression that is
represented in terms of a JsExpression object.
Note that data encoded as JSON must be UTF-8 encoded according to the JSON specification. You must ensure strings passed to this method have proper encoding before passing them.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The data to be encoded |
Returns
string: The encoding result
Throws
yii\base\InvalidArgumentException: if there is any encoding error
Since
2.0.4
handleJsonError()
Section titled “handleJsonError() ”protected static function handleJsonError(int $lastError): mixedHandles encode() and decode() errors by throwing exceptions with the respective error message.
Parameters
| Parameter | Type | Description |
|---|---|---|
$lastError | int | Error code from json_last_error(). |
Returns
mixed
Throws
yii\base\InvalidArgumentException: if there is any encoding/decoding error.
Since
2.0.6
processData()
Section titled “processData() ”protected static function processData(mixed $data, array &$expressions, string $expPrefix): mixedPre-processes the data before sending it to json_encode().
Parameters
| Parameter | Type | Description |
|---|---|---|
$data | mixed | The data to be processed |
$expressions | array | Collection of JavaScript expressions |
$expPrefix | string | A prefix internally used to handle JS expressions |
Returns
mixed: The processed data