Json
in package
Methods for handling JSON output, usually for AJAX responses
Table of Contents
Methods
- confirm() : never
- Sends a JSON confirmation response, and then terminates.
- decode() : mixed
- Decode a JSON string, with objects converted into arrays
- encode() : string
- Encode a json array as a string.
- encodeError() : array<string|int, mixed>
- Convert an error/exception into a JSON body.
- error() : never
- Sends a JSON message which has the success field set to zero and the specified message in the message field.
- out() : never
- Outputs the JSON data (encoded) + correct mime type, then stops script execution
Methods
confirm()
Sends a JSON confirmation response, and then terminates.
public
static confirm([mixed $data = null ]) : never
The returned data will have the key 'success' => 1 added. If no data is provided, the returned JSON will only contain the 'success' key.
This method halts execution
Parameters
- $data : mixed = null
Return values
never —This method echos and halts execution
decode()
Decode a JSON string, with objects converted into arrays
public
static decode(string $str[, int $flags = 0 ]) : mixed
Parameters
- $str : string
-
A JSON string. As per the spec, this should be UTF-8 encoded
- $flags : int = 0
-
Default JSON_INVALID_UTF8_SUBSTITUTE (if available)
Tags
Return values
mixed —The decoded value
encode()
Encode a json array as a string.
public
static encode(mixed $json[, bool|int $flags = 0 ]) : string
Parameters
- $json : mixed
- $flags : bool|int = 0
-
Applies pretty flags if
true.
Tags
Return values
stringencodeError()
Convert an error/exception into a JSON body.
public
static encodeError(Throwable $error[, bool $serialized = true ]) : array<string|int, mixed>
Parameters
- $error : Throwable
- $serialized : bool = true
-
use JsonSerializable if available.
Return values
array<string|int, mixed>error()
Sends a JSON message which has the success field set to zero and the specified message in the message field.
public
static error(mixed $message) : never
If $message is an Exception object, the message will be extracted and if on the test server, a stacktrace is included as well.
This method halts execution
Parameters
- $message : mixed
Return values
never —echos
out()
Outputs the JSON data (encoded) + correct mime type, then stops script execution
public
static out(mixed $data[, int $options = 0 ]) : never
Parameters
- $data : mixed
-
Any kind of data to be JSON encoded
- $options : int = 0
-
E.g. JSON_PRETTY_PRINT, as per http://php.net/manual/en/function.json-encode.php
Return values
never —This function calls echo