修正json_encode时 "Failed calling XXX::jsonSerialize()" 的异常

This commit is contained in:
yunwuxin
2016-10-17 17:52:09 +08:00
parent 63d6f49a1b
commit bb9e33e9a5
2 changed files with 31 additions and 15 deletions

View File

@@ -27,9 +27,11 @@ class Json extends Response
* @access protected * @access protected
* @param mixed $data 要处理的数据 * @param mixed $data 要处理的数据
* @return mixed * @return mixed
* @throws \Exception
*/ */
protected function output($data) protected function output($data)
{ {
try {
// 返回JSON数据格式到客户端 包含状态信息 // 返回JSON数据格式到客户端 包含状态信息
$data = json_encode($data, $this->options['json_encode_param']); $data = json_encode($data, $this->options['json_encode_param']);
@@ -38,6 +40,12 @@ class Json extends Response
} }
return $data; return $data;
} catch (\Exception $e) {
if ($e->getPrevious()) {
throw $e->getPrevious();
}
throw $e;
}
} }
} }

View File

@@ -30,9 +30,11 @@ class Jsonp extends Response
* @access protected * @access protected
* @param mixed $data 要处理的数据 * @param mixed $data 要处理的数据
* @return mixed * @return mixed
* @throws \Exception
*/ */
protected function output($data) protected function output($data)
{ {
try {
// 返回JSON数据格式到客户端 包含状态信息 [当url_common_param为false时是无法获取到$_GET的数据的故使用Request来获取<xiaobo.sun@qq.com>] // 返回JSON数据格式到客户端 包含状态信息 [当url_common_param为false时是无法获取到$_GET的数据的故使用Request来获取<xiaobo.sun@qq.com>]
$var_jsonp_handler = Request::instance()->param($this->options['var_jsonp_handler'], ""); $var_jsonp_handler = Request::instance()->param($this->options['var_jsonp_handler'], "");
$handler = !empty($var_jsonp_handler) ? $var_jsonp_handler : $this->options['default_jsonp_handler']; $handler = !empty($var_jsonp_handler) ? $var_jsonp_handler : $this->options['default_jsonp_handler'];
@@ -45,6 +47,12 @@ class Jsonp extends Response
$data = $handler . '(' . $data . ');'; $data = $handler . '(' . $data . ');';
return $data; return $data;
} catch (\Exception $e) {
if ($e->getPrevious()) {
throw $e->getPrevious();
}
throw $e;
}
} }
} }