diff --git a/reference/json/constants.xml b/reference/json/constants.xml index e1619d2cd8..94f0931a17 100644 --- a/reference/json/constants.xml +++ b/reference/json/constants.xml @@ -32,6 +32,17 @@ + + + JSON_ERROR_STATE_MISMATCH + (integer) + + + + Occurs with underflow or with the modes mismatch. + + + JSON_ERROR_CTRL_CHAR @@ -131,6 +142,18 @@ + + + JSON_NUMERIC_CHECK + (integer) + + + + Encodes numeric strings as numbers. + Available since PHP 5.3.3. + + + diff --git a/reference/json/functions/json-decode.xml b/reference/json/functions/json-decode.xml index 0a7c87e6cf..3499ec1c49 100644 --- a/reference/json/functions/json-decode.xml +++ b/reference/json/functions/json-decode.xml @@ -185,17 +185,18 @@ $json = json_encode( ); // Define the errors. -$json_errors = array( - JSON_ERROR_NONE => 'No error has occurred', - JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded', - JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded', - JSON_ERROR_SYNTAX => 'Syntax error', -); +$constants = get_defined_constants(true); +$json_errors = array(); +foreach ($constants["json"] as $name => $value) { + if (!strncmp($name, "JSON_ERROR_", 11)) { + $json_errors[$value] = $name; + } +} // Show the errors for different depths. foreach(range(4, 3, -1) as $depth) { var_dump(json_decode($json, True, $depth)); - echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL; + echo 'Last error: ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL; } ?> ]]> @@ -222,10 +223,10 @@ array(1) { } } } -Last error : No error has occurred +Last error: JSON_ERROR_NONE NULL -Last error : The maximum stack depth has been exceeded +Last error: JSON_ERROR_DEPTH ]]> diff --git a/reference/json/functions/json-encode.xml b/reference/json/functions/json-encode.xml index 212a10a90e..374211843c 100644 --- a/reference/json/functions/json-encode.xml +++ b/reference/json/functions/json-encode.xml @@ -43,6 +43,7 @@ JSON_HEX_TAG, JSON_HEX_AMP, JSON_HEX_APOS, + JSON_NUMERIC_CHECK, JSON_FORCE_OBJECT. diff --git a/reference/json/functions/json-last-error.xml b/reference/json/functions/json-last-error.xml index b98c3e6deb..c9abb680d0 100644 --- a/reference/json/functions/json-last-error.xml +++ b/reference/json/functions/json-last-error.xml @@ -50,13 +50,13 @@ - JSON_ERROR_CTRL_CHAR - Control character error, possibly incorrectly encoded + JSON_ERROR_STATE_MISMATCH + Invalid or malformed JSON - JSON_ERROR_STATE_MISMATCH - Invalid or malformed JSON + JSON_ERROR_CTRL_CHAR + Control character error, possibly incorrectly encoded @@ -97,17 +97,26 @@ foreach($json as $string) switch(json_last_error()) { + case JSON_ERROR_NONE: + echo ' - No errors'; + break; case JSON_ERROR_DEPTH: echo ' - Maximum stack depth exceeded'; break; + case JSON_ERROR_STATE_MISMATCH: + echo ' - Underflow or the modes mismatch'; + break; case JSON_ERROR_CTRL_CHAR: echo ' - Unexpected control character found'; break; case JSON_ERROR_SYNTAX: echo ' - Syntax error, malformed JSON'; break; - case JSON_ERROR_NONE: - echo ' - No errors'; + case JSON_ERROR_UTF8: + echo ' - Malformed UTF-8 characters, possibly incorrectly encoded'; + break; + default: + echo ' - Unknown error'; break; }