protected function encode($data, $options = 0)
    {
        // Check is already json string.
        if (is_string($data) && strlen($data) >= 1) {
            $firstChar = $data[0];

            if (in_array($firstChar, ['[', '{', '"'])) {
                return $data;
            }
        }

        // Clear json_last_error()
        json_encode(null);

        $json = json_encode($data, $options);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \UnexpectedValueException(sprintf('JSON encode failure: %s', json_last_error_msg()));
        }

        return $json;
    }
protected function encode($data, $options = 0)
    {
        // Check is already json string.
        if (is_string($data) && strlen($data) >= 1) {
            $firstChar = $data[0];

            if (in_array($firstChar, ['[', '{', '"'])) {
                return $data;
            }
        }

        // Clear json_last_error()
        json_encode(null);

        $json = json_encode($data, $options);

        if (json_last_error() !== JSON_ERROR_NONE) {
            throw new \UnexpectedValueException(sprintf('JSON encode failure: %s', json_last_error_msg()));
        }

        return $json;
    }
protected function encode ( $ data , $ options = 0 ) { // Check is already json string. if ( is_string ( $ data ) && strlen ( $ data ) >= 1 ) { $ firstChar = $ data [ 0 ] ; if ( in_array ( $ firstChar , [ '[' , '{' , '"' ] ) ) { return $ data ; } } // Clear json_last_error() json_encode ( null ) ; $ json = json_encode ( $ data , $ options ) ; if ( json_last_error ( ) !== JSON_ERROR_NONE ) { throw new \ UnexpectedValueException ( sprintf ( 'JSON encode failure: %s' , json_last_error_msg ( ) ) ) ; } return $ json ; }
Encode json.

@param   mixed $data    The dat to convert.
@param   int   $options The json_encode() options flag.

@return  string  Encoded json.
Encode json .