# BaseJson

> BaseJson provides concrete implementation for yii\helpers\Json.

[API reference](/docs/api/index.md) › [yii](/docs/api/yii/index.md) › [helpers](/docs/api/yii/helpers/index.md) › BaseJson

| class | `yii\helpers\BaseJson` |
| --- | --- |
| Since | 2.0 |
| Source | [`framework/helpers/BaseJson.php`](https://github.com/yiisoft/yii2/blob/22.0/framework/helpers/BaseJson.php#L25) |

1 subclass:

- [`Json`](/docs/api/yii/helpers/json/index.md)

BaseJson provides concrete implementation for [`Json`](/docs/api/yii/helpers/json/index.md).

Do not use BaseJson. Use [`Json`](/docs/api/yii/helpers/json/index.md) instead.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-jsonErrorMessages"></span>`$jsonErrorMessages` static | `array` | List of JSON Error messages assigned to constant names for better handling of PHP &lt;= 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`](#property-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`](#property-prettyPrint) static | `bool\|null` | Enables human readable output a.k.a. Default: `null`. Available since 2.0.43. | `BaseJson` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`decode()`](#method-decode) static | Decodes the given JSON string into a PHP data structure. | `BaseJson` |
| [`encode()`](#method-encode) static | Encodes the given value into a JSON string. | `BaseJson` |
| [`errorSummary()`](#method-errorSummary) static | Generates a summary of the validation errors. | `BaseJson` |
| [`htmlEncode()`](#method-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()`](#method-handleJsonError) protected static | Handles [`encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode) and [`decode()`](/docs/api/yii/helpers/basejson/index.md#method-decode) errors by throwing exceptions with the respective error message. | `BaseJson` |
| [`processData()`](#method-processData) protected static | Pre-processes the data before sending it to `json_encode()`. | `BaseJson` |

## Property details

### $keepObjectType <span id="property-keepObjectType"></span>

```php
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

### $prettyPrint <span id="property-prettyPrint"></span>

```php
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

## Method details

### decode() <span id="method-decode"></span>

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

Decodes 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`](/docs/api/yii/base/invalidargumentexception/index.md): if there is any decoding error

### encode() <span id="method-encode"></span>

```php
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`](/docs/api/yii/web/jsexpression/index.md) 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 \| JSON_UNESCAPED_UNICODE`. |

**Returns**

`string`: The encoding result.

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if there is any encoding error.

### errorSummary() <span id="method-errorSummary"></span>

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

Generates a summary of the validation errors.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$models` | [`yii\base\Model`](/docs/api/yii/base/model/index.md)`\|`[`yii\base\Model`](/docs/api/yii/base/model/index.md)`[]` | 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() <span id="method-htmlEncode"></span>

```php
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`](/docs/api/yii/web/jsexpression/index.md) 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`](/docs/api/yii/base/invalidargumentexception/index.md): if there is any encoding error

**Since**

2.0.4

### handleJsonError() <span id="method-handleJsonError"></span>

```php
protected static function handleJsonError(int $lastError): mixed
```

Handles [`encode()`](/docs/api/yii/helpers/basejson/index.md#method-encode) and [`decode()`](/docs/api/yii/helpers/basejson/index.md#method-decode) errors by throwing exceptions with the respective error message.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$lastError` | `int` | Error code from [json_last_error()](https://www.php.net/manual/en/function.json-last-error.php). |

**Returns**

`mixed`

**Throws**

- [`yii\base\InvalidArgumentException`](/docs/api/yii/base/invalidargumentexception/index.md): if there is any encoding/decoding error.

**Since**

2.0.6

### processData() <span id="method-processData"></span>

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

Pre-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