Json
- Type
- Class
- Namespace
- craft\helpers
- Inherits
- craft\helpers\Json » yii\helpers\Json (opens new window) » yii\helpers\BaseJson (opens new window)
- Since
- 3.0.0
Class Json
View source (opens new window)
# Public Properties
Property | Description |
---|---|
jsonErrorMessages (opens new window) | array (opens new window) – List of JSON Error messages assigned to constant names for better handling of PHP <= 5. |
keepObjectType (opens new window) | boolean (opens new window) – 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 (opens new window) | boolean (opens new window), null (opens new window) – Enables human readable output a. |
# Public Methods
Method | Description |
---|---|
decode() (opens new window) | 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. |
encode() | Encodes the given value into a JSON string. |
errorSummary() (opens new window) | Generates a summary of the validation errors. |
htmlEncode() (opens new window) | 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. |
# decodeFromFile()
- Since
- 4.3.5
Decodes JSON from a given file path.
View source (opens new window)
Arguments
$file
(string (opens new window)) – The file path$asArray
(boolean (opens new window)) – Whether to return objects in terms of associative arrays
Returns
mixed
– The JSON-decoded file contents
Throws
- yii\base\InvalidArgumentException (opens new window)
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.
View source (opens new window)
Arguments
$str
(mixed
) – The string to be decoded, if it's valid JSON.$asArray
(boolean (opens new window)) – Whether to return objects in terms of associative arrays.
Returns
mixed
– The PHP data, or the given string if it wasn’t valid JSON.
# 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.
View source (opens new window)
Arguments
$value
(mixed
) – The data to be encoded.$options
(integer (opens new window)) – The encoding options.JSON_UNESCAPED_UNICODE
is used by default.
Returns
string (opens new window) – The encoding result.
Throws
- yii\base\InvalidArgumentException (opens new window)
if there is any encoding error.
# isJsonObject()
- Since
- 3.5.0
Returns whether a string value looks like a JSON object or array.
View source (opens new window)
Arguments
$str
(string (opens new window))
Returns
# Protected Methods
Method | Description |
---|---|
handleJsonError() (opens new window) | Handles encode() (opens new window) and decode() (opens new window) errors by throwing exceptions with the respective error message. |
processData() (opens new window) | Pre-processes the data before sending it to json_encode() . |
← Install Localization →