JSON serializer: added exception info

This commit is contained in:
Marcin Kurczewski 2013-11-22 21:18:59 +01:00
parent c1e763316a
commit 0a5169a7d6

View file

@ -146,30 +146,26 @@ class TextHelper
public static function jsonEncode($obj, $illegalKeysRegex = '') public static function jsonEncode($obj, $illegalKeysRegex = '')
{ {
if (is_array($obj)) if (is_array($obj))
{ $set = function($key, $val) use ($obj) { $obj[$key] = $val; };
else
$set = function($key, $val) use ($obj) { $obj->$key = $val; };
foreach ($obj as $key => $val) foreach ($obj as $key => $val)
{ {
if ($val instanceof RedBean_OODBBean) if ($val instanceof RedBean_OODBBean)
{ {
$obj[$key] = R::exportAll($val); $set($key, R::exportAll($val));
} }
} elseif ($val instanceof Exception)
}
elseif (is_object($obj))
{ {
foreach ($obj as $key => $val) $set($key, ['message' => $val->getMessage(), 'trace' => $val->getTraceAsString()]);
{
if ($val instanceof RedBean_OODBBean)
{
$obj->$key = R::exportAll($val);
}
} }
} }
if (!empty($illegalKeysRegex)) if (!empty($illegalKeysRegex))
self::removeUnsafeKeys($obj, $illegalKeysRegex); self::removeUnsafeKeys($obj, $illegalKeysRegex);
return json_encode($obj); return json_encode($obj, JSON_UNESCAPED_UNICODE);
} }
public static function parseMarkdown($text, $inline = false) public static function parseMarkdown($text, $inline = false)