# Class yii\helpers\BaseJson

Inheritance | yii\helpers\BaseJson
Subclasses | yii\helpers\Json
Available since version | 2.0
Source Code | https://github.com/yiisoft/yii2/blob/master/framework/helpers/BaseJson.php

BaseJson provides concrete implementation for yii\helpers\Json.

Do not use BaseJson. Use yii\helpers\Json instead.

## Public Properties

Hide inherited properties

Property | Type | Description | Defined By
--- | --- | --- | ---
$jsonErrorMessages | array |  | yii\helpers\BaseJson

## Public Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
decode() | Decodes the given JSON string into a PHP data structure. | yii\helpers\BaseJson
encode() | Encodes the given value into a JSON string. | yii\helpers\BaseJson
errorSummary() | Generates a summary of the validation errors. | yii\helpers\BaseJson
htmlEncode() | Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. | yii\helpers\BaseJson

## Protected Methods

Hide inherited methods

Method | Description | Defined By
--- | --- | ---
handleJsonError() | Handles encode() and decode() errors by throwing exceptions with the respective error message. | yii\helpers\BaseJson
processData() | Pre-processes the data before sending it to `json_encode()`. | yii\helpers\BaseJson

## Property Details

$jsonErrorMessages public static property (available since version 2.0.7)
public static array $jsonErrorMessages = ['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_SYNTAX' => 'Syntax error.', 'JSON_ERROR_UTF8' => 'Malformed UTF-8 characters, possibly incorrectly encoded.', 'JSON_ERROR_RECURSION' => 'One or more recursive references in the value to be encoded.', 'JSON_ERROR_INF_OR_NAN' => 'One or more NAN or INF values in the value to be encoded', 'JSON_ERROR_UNSUPPORTED_TYPE' => 'A value of a type that cannot be encoded was given']

## Method Details

decode() public static method

Decodes the given JSON string into a PHP data structure.

public static mixed decode ( $json, $asArray = true )
$json | string | The JSON string to be decoded
$asArray | boolean | Whether to return objects in terms of associative arrays.
return | mixed | The PHP data
throws | yii\base\InvalidArgumentException | if there is any decoding error

encode() public static method

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 yii\web\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.

public static string encode ( $value, $options = 320 )
$value | mixed | The data to be encoded.
$options | integer | The encoding options. For more details please refer to https://secure.php.net/manual/en/function.json-encode.php. Default is `JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE`.
return | string | The encoding result.
throws | yii\base\InvalidArgumentException | if there is any encoding error.

errorSummary() public static method (available since version 2.0.14)

Generates a summary of the validation errors.

public static string errorSummary ( $models, $options = [] )
$models | yii\base\Model|yii\base\Model[] | 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`.
return | string | The generated error summary

handleJsonError() protected static method (available since version 2.0.6)

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

protected static void handleJsonError ( $lastError )
$lastError | integer | Error code from json_last_error().
throws | yii\base\InvalidArgumentException | if there is any encoding/decoding error.

htmlEncode() public static method (available since version 2.0.4)

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 yii\web\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.

public static string htmlEncode ( $value )
$value | mixed | The data to be encoded
return | string | The encoding result
throws | yii\base\InvalidArgumentException | if there is any encoding error

processData() protected static method

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

protected static mixed processData ( $data, &$expressions, $expPrefix )
$data | mixed | The data to be processed
$expressions | array | Collection of JavaScript expressions
$expPrefix | string | A prefix internally used to handle JS expressions
return | mixed | The processed data