JsonExpression
API reference › yii › db › JsonExpression
Class JsonExpression represents data that should be encoded to JSON.
For example:
new JsonExpression(['a' => 1, 'b' => 2]); // will be encoded to '{"a": 1, "b": 2}'Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$type protected | string| | Type of JSON, expression should be casted to. Default: null. | JsonExpression |
$value protected | mixed | The value to be encoded to JSON. Default: null. | JsonExpression |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | JsonExpression constructor. | JsonExpression |
getType() | JsonExpression | |
getValue() | JsonExpression | |
jsonSerialize() | Specify data which should be serialized to JSON | JsonExpression |
Constants
Section titled “Constants”| Constant | Value | Description | Defined by |
|---|---|---|---|
TYPE_JSON | 'json' | JsonExpression | |
TYPE_JSONB | 'jsonb' | JsonExpression |
Property details
Section titled “Property details”$type
Section titled “$type ”protected string|null $type = nullType of JSON, expression should be casted to.
Defaults to null, meaning
no explicit casting will be performed.
This property will be encountered only for DBMSs that support different types of JSON.
For example, PostgreSQL has json and jsonb types.
$value
Section titled “$value ”protected mixed $value = nullThe value to be encoded to JSON.
The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] input requirements.
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(mixed $value, string|null $type = null)JsonExpression constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$value | mixed | The value to be encoded to JSON. The value must be compatible with [\yii\helpers\Json::encode()|Json::encode()]] requirements. |
$type | string| | The type of the JSON. See JsonExpression::$type |
See also
getType()
Section titled “getType() ”public function getType(): string|nullgetValue()
Section titled “getValue() ”public function getValue(): mixedjsonSerialize()
Section titled “jsonSerialize() ”public function jsonSerialize(): mixedSpecify data which should be serialized to JSON
Returns
mixed: Data which can be serialized by <b>json_encode</b>, which is a value of any type other than a resource.
Throws
yii\base\InvalidConfigException: when JsonExpression contains QueryInterface object
Since
2.0.14.2