Json ​
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\Json » yii\helpers\Json » yii\helpers\BaseJson
- Since
- 3.0.0
Class Json
Public Properties ​
| Property | Description |
|---|---|
| jsonErrorMessages | array – List of JSON Error messages assigned to constant names for better handling of PHP <= 5.5. |
| keepObjectType | boolean – 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. |
| prettyPrint | boolean, null – Enables human readable output a.k.a. |
Public Methods ​
| Method | Description |
|---|---|
| decode() | Decodes the given JSON string into a PHP data structure. |
| decodeFromFile() | Decodes JSON from a given file path. |
| decodeIfJson() | Decodes the given JSON string into a PHP data structure, only if the string is valid JSON. |
| detectIndent() | Detects and returns the indentation sequence used by the given JSON string. |
| encode() | Encodes the given value into a JSON string. |
| encodeToFile() | Writes out a JSON file for the given value, maintaining its current indentation sequence if the file already exists. |
| errorSummary() | Generates a summary of the validation errors. |
| htmlEncode() | Encodes the given value into a JSON string HTML-escaping entities so it is safe to be embedded in HTML code. |
| isJsonObject() | Returns whether a string value looks like a JSON object or array. |
| reindent() | Re-indents JSON with the given indentation string. |
decodeFromFile() ​
- Since
- 4.3.5
Decodes JSON from a given file path.
Arguments ​
$file(string) – The file path$asArray(boolean) – Whether to return objects in terms of associative arrays
Returns ​
mixed – The JSON-decoded file contents
Throws ​
- yii\base\InvalidArgumentException
if the file doesn’t exist or there was a problem JSON-decoding it
decodeIfJson() ​
Decodes the given JSON string into a PHP data structure, only if the string is valid JSON.
Arguments ​
$str(mixed) – The string to be decoded, if it's valid JSON.$asArray(boolean) – Whether to return objects in terms of associative arrays.
Returns ​
mixed – The PHP data, or the given string if it wasn’t valid JSON.
detectIndent() ​
- Since
- 4.16.6
Detects and returns the indentation sequence used by the given JSON string.
Arguments ​
$json(string)
Returns ​
encode() ​
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 \craft\helpers\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.
Arguments ​
$value(mixed) – The data to be encoded.$options(integer) – The encoding options.JSON_UNESCAPED_UNICODEis used by default.
Returns ​
string – The encoding result.
Throws ​
- yii\base\InvalidArgumentException
if there is any encoding error.
encodeToFile() ​
- Since
- 4.16.6
Writes out a JSON file for the given value, maintaining its current indentation sequence if the file already exists.
Arguments ​
$path(string) – The file path$value(mixed) – The data to be encoded.$options(integer) – The encoding options.JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINTis used by default.$defaultIndent(string) – The default indentation sequence to use if the file doesn’t exist
isJsonObject() ​
- Since
- 3.5.0
Returns whether a string value looks like a JSON object or array.
Arguments ​
$str(string)
Returns ​
reindent() ​
- Since
- 4.16.6
Re-indents JSON with the given indentation string.
Arguments ​
Returns ​
Protected Methods ​
| Method | Description |
|---|---|
| handleJsonError() | Handles encode() and decode() errors by throwing exceptions with the respective error message. |
| processData() | Pre-processes the data before sending it to json_encode(). |