@@ -55,7 +55,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
5555 ZEND_ARG_INFO (0 , options )
5656ZEND_END_ARG_INFO ()
5757
58- ZEND_BEGIN_ARG_INFO (arginfo_json_last_error , 0 )
58+ ZEND_BEGIN_ARG_INFO_EX (arginfo_json_last_error , 0 , 0 , 0 )
59+ ZEND_ARG_INFO (0 , as_string )
5960ZEND_END_ARG_INFO ()
6061/* }}} */
6162
@@ -236,7 +237,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
236237
237238 if (myht && myht -> nApplyCount > 1 ) {
238239 JSON_G (error_code ) = PHP_JSON_ERROR_RECURSION ;
239- php_error_docref (NULL TSRMLS_CC , E_WARNING , "recursion detected ");
240240 smart_str_appendl (buf , "null" , 4 );
241241 return ;
242242 }
@@ -378,7 +378,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
378378 efree (tmp );
379379 } else {
380380 JSON_G (error_code ) = PHP_JSON_ERROR_INF_OR_NAN ;
381- php_error_docref (NULL TSRMLS_CC , E_WARNING , "double %.9g does not conform to the JSON spec" , d );
382381 smart_str_appendc (buf , '0' );
383382 }
384383 }
@@ -395,7 +394,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
395394 }
396395 if (ulen < 0 ) {
397396 JSON_G (error_code ) = PHP_JSON_ERROR_UTF8 ;
398- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Invalid UTF-8 sequence in argument" );
399397 smart_str_appendl (buf , "null" , 4 );
400398 } else {
401399 smart_str_appendl (buf , "\"\"" , 2 );
@@ -527,7 +525,6 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio
527525
528526 if (myht && myht -> nApplyCount > 1 ) {
529527 JSON_G (error_code ) = PHP_JSON_ERROR_RECURSION ;
530- php_error_docref (NULL TSRMLS_CC , E_WARNING , "recursion detected ");
531528 smart_str_appendl (buf , "null" , 4 );
532529 return ;
533530 }
@@ -592,7 +589,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
592589 efree (d );
593590 } else {
594591 JSON_G (error_code ) = PHP_JSON_ERROR_INF_OR_NAN ;
595- php_error_docref (NULL TSRMLS_CC , E_WARNING , "double %.9g does not conform to the JSON spec" , dbl );
596592 smart_str_appendc (buf , '0' );
597593 }
598594 }
@@ -614,7 +610,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
614610
615611 default :
616612 JSON_G (error_code ) = PHP_JSON_ERROR_UNSUPPORTED_TYPE ;
617- php_error_docref (NULL TSRMLS_CC , E_WARNING , "type is unsupported ");
618613 smart_str_appendl (buf , "null" , 4 );
619614 break ;
620615 }
@@ -754,11 +749,40 @@ static PHP_FUNCTION(json_decode)
754749 Returns the error code of the last json_decode(). */
755750static PHP_FUNCTION (json_last_error )
756751{
757- if (zend_parse_parameters_none () == FAILURE ) {
752+ zend_bool as_string = 0 ;
753+
754+ if (zend_parse_parameters (ZEND_NUM_ARGS () TSRMLS_CC , "|b" , & as_string ) == FAILURE ) {
758755 return ;
759756 }
760757
761- RETURN_LONG (JSON_G (error_code ));
758+ /* return error code (JSON_ERROR_* constants) */
759+ if (!as_string ) {
760+ RETURN_LONG (JSON_G (error_code ));
761+ }
762+
763+ /* return error message (for debugging purposes) */
764+ switch (JSON_G (error_code )) {
765+ case PHP_JSON_ERROR_NONE :
766+ RETURN_STRING ("No error" , 1 );
767+ case PHP_JSON_ERROR_DEPTH :
768+ RETURN_STRING ("Maximum stack depth exceeded" , 1 );
769+ case PHP_JSON_ERROR_STATE_MISMATCH :
770+ RETURN_STRING ("State mismatch (invalid or malformed JSON)" , 1 );
771+ case PHP_JSON_ERROR_CTRL_CHAR :
772+ RETURN_STRING ("Control character error, possibly incorrectly encoded" , 1 );
773+ case PHP_JSON_ERROR_SYNTAX :
774+ RETURN_STRING ("Syntax error" , 1 );
775+ case PHP_JSON_ERROR_UTF8 :
776+ RETURN_STRING ("Malformed UTF-8 characters, possibly incorrectly encoded" , 1 );
777+ case PHP_JSON_ERROR_RECURSION :
778+ RETURN_STRING ("Recursion detected" , 1 );
779+ case PHP_JSON_ERROR_INF_OR_NAN :
780+ RETURN_STRING ("Inf and NaN cannot be JSON encoded" , 1 );
781+ case PHP_JSON_ERROR_UNSUPPORTED_TYPE :
782+ RETURN_STRING ("Type is not supported" , 1 );
783+ default :
784+ RETURN_STRING ("Unknown error" , 1 );
785+ }
762786}
763787/* }}} */
764788
0 commit comments