]> granicus.if.org Git - php/commitdiff
Improve JSON error handling
authorNikita Popov <nikic@php.net>
Wed, 27 Jun 2012 10:19:41 +0000 (12:19 +0200)
committerNikita Popov <nikic@php.net>
Wed, 27 Jun 2012 10:21:48 +0000 (12:21 +0200)
json_encode() no longer throws warnings. Instead only the error code for
json_last_error() is set.

As it is hard to debug the error from just an error code an optional
$as_string parameter was added to json_last_error(), which returns an
error message instead of an error code.

NEWS
ext/json/json.c
ext/json/tests/003.phpt
ext/json/tests/004.phpt
ext/json/tests/bug54058.phpt
ext/json/tests/bug61537.phpt
ext/json/tests/inf_nan_error.phpt
ext/json/tests/json_encode_basic.phpt
ext/json/tests/unsupported_type_error.phpt

diff --git a/NEWS b/NEWS
index 63e91f5c6bcc4eeb1974a83bde6b7e684ab195c9..79db5c6b7bf31975e2f6b15bde11082b73b0a4be 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -41,6 +41,9 @@ PHP                                                                        NEWS
   . Fixed bug #62017 (datefmt_create with incorrectly encoded timezone leaks
     pattern). (Gustavo)
   . Fixed bug #60785 (memory leak in IntlDateFormatter constructor). (Gustavo)
+
+- JSON:
+  . Improved error handling. (Nikita Popov)
   
 - PDO:
   . Fixed bug #61755 (A parsing bug in the prepared statements can lead to
index a90476530e8d3dcab2a00b78c2a614e59ae08f35..5e0351f3f1e0df130eab02e87bf89422e4adb0c9 100644 (file)
@@ -51,7 +51,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
        ZEND_ARG_INFO(0, depth)
 ZEND_END_ARG_INFO()
 
-ZEND_BEGIN_ARG_INFO(arginfo_json_last_error, 0)
+ZEND_BEGIN_ARG_INFO_EX(arginfo_json_last_error, 0, 0, 0)
+       ZEND_ARG_INFO(0, as_string)
 ZEND_END_ARG_INFO()
 /* }}} */
 
@@ -185,7 +186,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
 
        if (myht && myht->nApplyCount > 1) {
                JSON_G(error_code) = PHP_JSON_ERROR_RECURSION;
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "recursion detected");
                smart_str_appendl(buf, "null", 4);
                return;
        }
@@ -308,7 +308,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
                                        efree(tmp);
                                } else {
                                        JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
-                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", d);
                                        smart_str_appendc(buf, '0');
                                }
                        }
@@ -326,7 +325,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
                }
                if (len < 0) {
                        JSON_G(error_code) = PHP_JSON_ERROR_UTF8;
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "Invalid UTF-8 sequence in argument");
                        smart_str_appendl(buf, "null", 4);
                } else {
                        smart_str_appendl(buf, "\"\"", 2);
@@ -466,7 +464,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
                                        efree(d);
                                } else {
                                        JSON_G(error_code) = PHP_JSON_ERROR_INF_OR_NAN;
-                                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "double %.9g does not conform to the JSON spec", dbl);
                                        smart_str_appendc(buf, '0');
                                }
                        }
@@ -483,7 +480,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
 
                default:
                        JSON_G(error_code) = PHP_JSON_ERROR_UNSUPPORTED_TYPE;
-                       php_error_docref(NULL TSRMLS_CC, E_WARNING, "type is unsupported");
                        smart_str_appendl(buf, "null", 4);
                        break;
        }
@@ -614,11 +610,40 @@ static PHP_FUNCTION(json_decode)
    Returns the error code of the last json_decode(). */
 static PHP_FUNCTION(json_last_error)
 {
-       if (zend_parse_parameters_none() == FAILURE) {
+       zend_bool as_string = 0;
+
+       if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &as_string) == FAILURE) {
                return;
        }
 
-       RETURN_LONG(JSON_G(error_code));
+       /* return error code (JSON_ERROR_* constants) */
+       if (!as_string) {
+               RETURN_LONG(JSON_G(error_code));
+       }
+
+       /* return error message (for debugging purposes) */
+       switch(JSON_G(error_code)) {
+               case PHP_JSON_ERROR_NONE:
+                       RETURN_STRING("No error", 1);
+               case PHP_JSON_ERROR_DEPTH:
+                       RETURN_STRING("Maximum stack depth exceeded", 1);
+               case PHP_JSON_ERROR_STATE_MISMATCH:
+                       RETURN_STRING("State mismatch (invalid or malformed JSON)", 1);
+               case PHP_JSON_ERROR_CTRL_CHAR:
+                       RETURN_STRING("Control character error, possibly incorrectly encoded", 1);
+               case PHP_JSON_ERROR_SYNTAX:
+                       RETURN_STRING("Syntax error", 1);
+               case PHP_JSON_ERROR_UTF8:
+                       RETURN_STRING("Malformed UTF-8 characters, possibly incorrectly encoded", 1);
+               case PHP_JSON_ERROR_RECURSION:
+                       RETURN_STRING("Recursion detected", 1);
+               case PHP_JSON_ERROR_INF_OR_NAN:
+                       RETURN_STRING("Inf and NaN cannot be JSON encoded", 1);
+               case PHP_JSON_ERROR_UNSUPPORTED_TYPE:
+                       RETURN_STRING("Type is not supported", 1);
+               default:
+                       RETURN_STRING("Unknown error", 1);
+       }
 }
 /* }}} */
 
index ab637110087dc3dcb9496f5a22621ebe4bff7fdf..71874525a701b2f1b88774f87830af354b42a133 100644 (file)
@@ -10,11 +10,17 @@ $a[] = &$a;
 
 var_dump($a);
 
+echo "\n";
+
 var_dump(json_encode($a));
 var_dump(json_last_error());
+var_dump(json_last_error(true));
+
+echo "\n";
 
 var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
 var_dump(json_last_error());
+var_dump(json_last_error(true));
 
 echo "Done\n";
 ?>
@@ -27,11 +33,11 @@ array(1) {
   }
 }
 
-Warning: json_encode(): recursion detected in %s on line %d
 bool(false)
 int(6)
+string(%d) "Recursion detected"
 
-Warning: json_encode(): recursion detected in %s on line %d
 string(8) "[[null]]"
 int(6)
+string(%d) "Recursion detected"
 Done
index 9f9abfe46a6466dc6bcc06bac3ee4b996b55fbd4..49c543edcae6dcb06d83bf85dd7eb1ad4e2b5d1d 100644 (file)
@@ -10,11 +10,17 @@ $a->prop = $a;
 
 var_dump($a);
 
+echo "\n";
+
 var_dump(json_encode($a));
 var_dump(json_last_error());
+var_dump(json_last_error(true));
+
+echo "\n";
 
 var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
 var_dump(json_last_error());
+var_dump(json_last_error(true));
 
 echo "Done\n";
 ?>
@@ -24,11 +30,11 @@ object(stdClass)#%d (1) {
   *RECURSION*
 }
 
-Warning: json_encode(): recursion detected in %s on line %d
 bool(false)
 int(6)
+string(%d) "Recursion detected"
 
-Warning: json_encode(): recursion detected in %s on line %d
 string(22) "{"prop":{"prop":null}}"
 int(6)
+string(%d) "Recursion detected"
 Done
index 08c7f579ab90b2344078832cc1638064f14230d3..2c2304578e21361a1984886622148b238f1c23ac 100644 (file)
@@ -9,17 +9,20 @@ $bad_utf8 = quoted_printable_decode('=B0');
 
 json_encode($bad_utf8);
 var_dump(json_last_error());
+var_dump(json_last_error(true));
 
 $a = new stdclass;
 $a->foo = quoted_printable_decode('=B0');
 json_encode($a);
 var_dump(json_last_error());
+var_dump(json_last_error(true));
 
 $b = new stdclass;
 $b->foo = $bad_utf8;
 $b->bar = 1;
 json_encode($b);
 var_dump(json_last_error());
+var_dump(json_last_error(true));
 
 $c = array(
     'foo' => $bad_utf8,
@@ -27,16 +30,15 @@ $c = array(
 );
 json_encode($c);
 var_dump(json_last_error());
+var_dump(json_last_error(true));
+
 ?>
 --EXPECTF--
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
index e2abdda66a3d6dc9599e573e7e1fff7d20615c8f..f6bb02bae451dc81932bb4042286cebd309d26cb 100644 (file)
@@ -5,26 +5,35 @@ Bug #61537 (json_encode() incorrectly truncates/discards information)
 --FILE--
 <?php
 $invalid_utf8 = "\x9f";
-var_dump(json_encode($invalid_utf8), json_last_error());
-var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
+
+var_dump(json_encode($invalid_utf8));
+var_dump(json_last_error(), json_last_error(true));
+
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error(), json_last_error(true));
+
+echo "\n";
 
 $invalid_utf8 = "an invalid sequen\xce in the middle of a string";
-var_dump(json_encode($invalid_utf8), json_last_error());
-var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR), json_last_error());
+
+var_dump(json_encode($invalid_utf8));
+var_dump(json_last_error(), json_last_error(true));
+
+var_dump(json_encode($invalid_utf8, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error(), json_last_error(true));
+
 ?>
 --EXPECTF--
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 bool(false)
 int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 string(4) "null"
 int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
 bool(false)
 int(5)
-
-Warning: json_encode(): Invalid UTF-8 sequence in argument in %s on line %d
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
 string(4) "null"
 int(5)
+string(56) "Malformed UTF-8 characters, possibly incorrectly encoded"
index a3ed5e7b882945e801f8ea54f5deda08802be330..f12e902d9f0e226f4fda969751cc9fd2d93ef41c 100644 (file)
@@ -8,37 +8,36 @@ $inf = INF;
 var_dump($inf);
 
 var_dump(json_encode($inf));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
 
 var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
+
+echo "\n";
 
 $nan = NAN;
 
 var_dump($nan);
 
 var_dump(json_encode($nan));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
 
 var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
 ?>
 --EXPECTF--
 float(INF)
-
-Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d
 bool(false)
 int(7)
-
-Warning: json_encode(): double INF does not conform to the JSON spec in %s on line %d
+string(34) "Inf and NaN cannot be JSON encoded"
 string(1) "0"
 int(7)
-float(NAN)
+string(34) "Inf and NaN cannot be JSON encoded"
 
-Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
+float(NAN)
 bool(false)
 int(7)
-
-Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d
+string(34) "Inf and NaN cannot be JSON encoded"
 string(1) "0"
 int(7)
+string(34) "Inf and NaN cannot be JSON encoded"
index 7ee68c58ca89aefad159a8716a3a248411485a11..fc348eed8112c560e825c2190e797b12e43d2cd8 100644 (file)
@@ -150,8 +150,6 @@ string(4) "null"
 -- Iteration 25 --
 string(4) "null"
 -- Iteration 26 --
-
-Warning: json_encode(): type is unsupported in %s on line %d
 bool(false)
 -- Iteration 27 --
 string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}"
index 2564c6a3c8c614c435ff0410491a28925f3bfc81..f36afb44a5e90872dfae0781580e6ac004da2977 100644 (file)
@@ -8,19 +8,17 @@ $resource = fopen(__FILE__, "r");
 var_dump($resource);
 
 var_dump(json_encode($resource));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
 
 var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR));
-var_dump(json_last_error());
+var_dump(json_last_error(), json_last_error(true));
 
 ?>
 --EXPECTF--
 resource(5) of type (stream)
-
-Warning: json_encode(): type is unsupported in %s on line %d
 bool(false)
 int(8)
-
-Warning: json_encode(): type is unsupported in %s on line %d
+string(21) "Type is not supported"
 string(4) "null"
 int(8)
+string(21) "Type is not supported"