Skip to content
PHP Ecosystem
Explore

BaseJson

API reference › yii › helpers › BaseJson

classyii\helpers\BaseJson
Since2.0
Sourceframework/helpers/BaseJson.php
1 subclass

BaseJson provides concrete implementation for Json.

Do not use BaseJson. Use Json instead.

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
public static bool $keepObjectType = false

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.

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

public static bool|null $prettyPrint = null

Enables 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

public static function decode(string $json, bool $asArray = true): mixed

Decodes the given JSON string into a PHP data structure.

Parameters

ParameterTypeDescription
$jsonstringThe JSON string to be decoded
$asArrayboolWhether to return objects in terms of associative arrays.

Returns

mixed: The PHP data

Throws

public static function encode(mixed $value, int $options = 320): string

Encodes 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

ParameterTypeDescription
$valuemixedThe data to be encoded.
$optionsintThe encoding options. For more details please refer to https://www.php.net/manual/en/function.json-encode.php. Default is JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE.

Returns

string: The encoding result.

Throws

public static function errorSummary(yii\base\Model|yii\base\Model[] $models, array $options = []): string

Generates a summary of the validation errors.

Parameters

ParameterTypeDescription
$modelsyii\base\Model|yii\base\Model[]The model(s) whose validation errors are to be displayed.
$optionsarrayThe 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

public static function htmlEncode(mixed $value): string

Encodes 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

ParameterTypeDescription
$valuemixedThe data to be encoded

Returns

string: The encoding result

Throws

Since

2.0.4

protected static function handleJsonError(int $lastError): mixed

Handles encode() and decode() errors by throwing exceptions with the respective error message.

Parameters

ParameterTypeDescription
$lastErrorintError code from json_last_error().

Returns

mixed

Throws

Since

2.0.6

protected static function processData(mixed $data, array &$expressions, string $expPrefix): mixed

Pre-processes the data before sending it to json_encode().

Parameters

ParameterTypeDescription
$datamixedThe data to be processed
$expressionsarrayCollection of JavaScript expressions
$expPrefixstringA prefix internally used to handle JS expressions

Returns

mixed: The processed data