@@ -55,7 +55,8 @@ ZEND_BEGIN_ARG_INFO_EX(arginfo_json_decode, 0, 0, 1)
55
55
ZEND_ARG_INFO (0 , options )
56
56
ZEND_END_ARG_INFO ()
57
57
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 )
59
60
ZEND_END_ARG_INFO ()
60
61
/* }}} */
61
62
@@ -236,7 +237,6 @@ static void json_encode_array(smart_str *buf, zval **val, int options TSRMLS_DC)
236
237
237
238
if (myht && myht -> nApplyCount > 1 ) {
238
239
JSON_G (error_code ) = PHP_JSON_ERROR_RECURSION ;
239
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "recursion detected ");
240
240
smart_str_appendl (buf , "null" , 4 );
241
241
return ;
242
242
}
@@ -378,7 +378,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
378
378
efree (tmp );
379
379
} else {
380
380
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 );
382
381
smart_str_appendc (buf , '0' );
383
382
}
384
383
}
@@ -395,7 +394,6 @@ static void json_escape_string(smart_str *buf, char *s, int len, int options TSR
395
394
}
396
395
if (ulen < 0 ) {
397
396
JSON_G (error_code ) = PHP_JSON_ERROR_UTF8 ;
398
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "Invalid UTF-8 sequence in argument" );
399
397
smart_str_appendl (buf , "null" , 4 );
400
398
} else {
401
399
smart_str_appendl (buf , "\"\"" , 2 );
@@ -527,7 +525,6 @@ static void json_encode_serializable_object(smart_str *buf, zval *val, int optio
527
525
528
526
if (myht && myht -> nApplyCount > 1 ) {
529
527
JSON_G (error_code ) = PHP_JSON_ERROR_RECURSION ;
530
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "recursion detected ");
531
528
smart_str_appendl (buf , "null" , 4 );
532
529
return ;
533
530
}
@@ -592,7 +589,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
592
589
efree (d );
593
590
} else {
594
591
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 );
596
592
smart_str_appendc (buf , '0' );
597
593
}
598
594
}
@@ -614,7 +610,6 @@ PHP_JSON_API void php_json_encode(smart_str *buf, zval *val, int options TSRMLS_
614
610
615
611
default :
616
612
JSON_G (error_code ) = PHP_JSON_ERROR_UNSUPPORTED_TYPE ;
617
- php_error_docref (NULL TSRMLS_CC , E_WARNING , "type is unsupported ");
618
613
smart_str_appendl (buf , "null" , 4 );
619
614
break ;
620
615
}
@@ -754,11 +749,40 @@ static PHP_FUNCTION(json_decode)
754
749
Returns the error code of the last json_decode(). */
755
750
static PHP_FUNCTION (json_last_error )
756
751
{
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 ) {
758
755
return ;
759
756
}
760
757
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
+ }
762
786
}
763
787
/* }}} */
764
788
0 commit comments