Format::to_json PHP Method

to_json() public method

Encode data as json
public to_json ( mixed | null $data = NULL ) : string
$data mixed | null Optional data to pass, so as to override the data passed to the constructor
return string Json representation of a value
    public function to_json($data = NULL)
    {
        // If no data is passed as a parameter, then use the data passed
        // via the constructor
        if ($data === NULL && func_num_args() === 0) {
            $data = $this->_data;
        }
        // Get the callback parameter (if set)
        $callback = $this->_CI->input->get('callback');
        if (empty($callback) === TRUE) {
            return json_encode($data);
        } elseif (preg_match('/^[a-z_\\$][a-z0-9\\$_]*(\\.[a-z_\\$][a-z0-9\\$_]*)*$/i', $callback)) {
            // Return the data as encoded json with a callback
            return $callback . '(' . json_encode($data) . ');';
        }
        // An invalid jsonp callback function provided.
        // Though I don't believe this should be hardcoded here
        $data['warning'] = 'INVALID JSONP CALLBACK: ' . $callback;
        return json_encode($data);
    }