note 89461 added to function.json-last-error
| From: | lior at mytopia dot com | Date: | Mon, 09 Mar 2009 13:17:14 +0000 |
| Subject: | note 89461 added to function.json-last-error | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-151646@lists.php.net to get a copy of this message | ||
For those of you who prefer a more object oriented approach (as I do), here is a fairly simple
wrapper that handles errors using exceptions:
<?php
class JSON
{
public static function Encode($obj)
{
return json_encode($obj);
}
public static function Decode($json, $toAssoc = false)
{
$result = json_decode($json, $toAssoc);
switch(json_last_error())
{
case JSON_ERROR_DEPTH:
$error = ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_CTRL_CHAR:
$error = ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
$error = ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_NONE:
default:
$error = '';
}
if (!empty($error))
throw new Exception('JSON Error: '.$error);
return $result;
}
}
?>
----
Server IP: 216.92.131.65
Probable Submitter: 62.219.192.12
----
Manual Page -- http://www.php.net/manual/en/function.json-last-error.php
Edit -- https://master.php.net/note/edit/89461
Del: integrated -- https://master.php.net/note/delete/89461/integrated
Del: useless -- https://master.php.net/note/delete/89461/useless
Del: bad code -- https://master.php.net/note/delete/89461/bad+code
Del: spam -- https://master.php.net/note/delete/89461/spam
Del: non-english -- https://master.php.net/note/delete/89461/non-english
Del: in docs -- https://master.php.net/note/delete/89461/in+docs
Del: other reasons-- https://master.php.net/note/delete/89461
Reject -- https://master.php.net/note/reject/89461
Search -- https://master.php.net/manage/user-notes.php