Browse Source
Improve json_encode error handling
Improve json_encode error handling
json_encode() now returns bool(false) for all possible errors, throws the respective warning and also sets the respective json_last_error() error code. Three new error codes have been added: * JSON_ERROR_RECURSION * JSON_ERROR_INF_OR_NAN * JSON_ERROR_UNSUPPORTED_TYPE To get a partial JSON output instead of bool(false) the option JSON_PARTIAL_OUTPUT_ON_ERROR can be specified. In this case the invalid segments will be replaced either by null (for recursion, unsupported type and invalid JSON) or 0 (for Inf and NaN). The warning for invalid UTF-8 stays intact and is thrown also with display_errors = On. If this behavior is undesired this can be remedied later.PHP-5.3.15
9 changed files with 110 additions and 13 deletions
-
5ext/json/JSON_parser.h
-
15ext/json/json.c
-
11ext/json/tests/003.phpt
-
10ext/json/tests/004.phpt
-
44ext/json/tests/inf_nan_error.phpt
-
4ext/json/tests/json_encode_basic.phpt
-
4ext/json/tests/pass001.1.phpt
-
4ext/json/tests/pass001.phpt
-
26ext/json/tests/unsupported_type_error.phpt
@ -0,0 +1,44 @@ |
|||
--TEST-- |
|||
An error is thrown when INF or NaN are encoded |
|||
--FILE-- |
|||
<?php |
|||
|
|||
$inf = INF; |
|||
|
|||
var_dump($inf); |
|||
|
|||
var_dump(json_encode($inf)); |
|||
var_dump(json_last_error()); |
|||
|
|||
var_dump(json_encode($inf, JSON_PARTIAL_OUTPUT_ON_ERROR)); |
|||
var_dump(json_last_error()); |
|||
|
|||
$nan = NAN; |
|||
|
|||
var_dump($nan); |
|||
|
|||
var_dump(json_encode($nan)); |
|||
var_dump(json_last_error()); |
|||
|
|||
var_dump(json_encode($nan, JSON_PARTIAL_OUTPUT_ON_ERROR)); |
|||
var_dump(json_last_error()); |
|||
?> |
|||
--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(1) "0" |
|||
int(7) |
|||
float(NAN) |
|||
|
|||
Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d |
|||
bool(false) |
|||
int(7) |
|||
|
|||
Warning: json_encode(): double NAN does not conform to the JSON spec in %s on line %d |
|||
string(1) "0" |
|||
int(7) |
|||
@ -0,0 +1,26 @@ |
|||
--TEST-- |
|||
An error is thrown when an unsupported type is encoded |
|||
--FILE-- |
|||
<?php |
|||
|
|||
$resource = fopen(__FILE__, "r"); |
|||
|
|||
var_dump($resource); |
|||
|
|||
var_dump(json_encode($resource)); |
|||
var_dump(json_last_error()); |
|||
|
|||
var_dump(json_encode($resource, JSON_PARTIAL_OUTPUT_ON_ERROR)); |
|||
var_dump(json_last_error()); |
|||
|
|||
?> |
|||
--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(4) "null" |
|||
int(8) |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue