summaryrefslogtreecommitdiff
path: root/ext/json/tests
diff options
context:
space:
mode:
authorNikita Popov <nikic@php.net>2012-06-23 20:46:27 +0200
committerNikita Popov <nikic@php.net>2012-06-23 20:51:52 +0200
commit84fe2cc890e49f40bac7c3ba74b3cfc6dc4cef2f (patch)
tree4b07a64554232483751acac245a9eb43154d0ecc /ext/json/tests
parentcc90ac54beb7359e5a3210261ce09159bbc43e92 (diff)
downloadphp-git-84fe2cc890e49f40bac7c3ba74b3cfc6dc4cef2f.tar.gz
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.
Diffstat (limited to 'ext/json/tests')
-rw-r--r--ext/json/tests/003.phpt11
-rw-r--r--ext/json/tests/004.phpt10
-rw-r--r--ext/json/tests/inf_nan_error.phpt44
-rw-r--r--ext/json/tests/json_encode_basic.phpt4
-rw-r--r--ext/json/tests/pass001.1.phpt4
-rw-r--r--ext/json/tests/pass001.phpt4
-rw-r--r--ext/json/tests/unsupported_type_error.phpt26
7 files changed, 95 insertions, 8 deletions
diff --git a/ext/json/tests/003.phpt b/ext/json/tests/003.phpt
index 3b52fb0884..ab63711008 100644
--- a/ext/json/tests/003.phpt
+++ b/ext/json/tests/003.phpt
@@ -9,10 +9,12 @@ $a = array();
$a[] = &$a;
var_dump($a);
+
var_dump(json_encode($a));
+var_dump(json_last_error());
-/* Break circular data structure to prevent memory leaks */
-unset($a[0]);
+var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error());
echo "Done\n";
?>
@@ -26,5 +28,10 @@ array(1) {
}
Warning: json_encode(): recursion detected in %s on line %d
+bool(false)
+int(6)
+
+Warning: json_encode(): recursion detected in %s on line %d
string(8) "[[null]]"
+int(6)
Done
diff --git a/ext/json/tests/004.phpt b/ext/json/tests/004.phpt
index 1d282f9a96..9f9abfe46a 100644
--- a/ext/json/tests/004.phpt
+++ b/ext/json/tests/004.phpt
@@ -9,7 +9,12 @@ $a = new stdclass;
$a->prop = $a;
var_dump($a);
+
var_dump(json_encode($a));
+var_dump(json_last_error());
+
+var_dump(json_encode($a, JSON_PARTIAL_OUTPUT_ON_ERROR));
+var_dump(json_last_error());
echo "Done\n";
?>
@@ -20,5 +25,10 @@ object(stdClass)#%d (1) {
}
Warning: json_encode(): recursion detected in %s on line %d
+bool(false)
+int(6)
+
+Warning: json_encode(): recursion detected in %s on line %d
string(22) "{"prop":{"prop":null}}"
+int(6)
Done
diff --git a/ext/json/tests/inf_nan_error.phpt b/ext/json/tests/inf_nan_error.phpt
new file mode 100644
index 0000000000..a3ed5e7b88
--- /dev/null
+++ b/ext/json/tests/inf_nan_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)
diff --git a/ext/json/tests/json_encode_basic.phpt b/ext/json/tests/json_encode_basic.phpt
index 003fcd44c6..7ee68c58ca 100644
--- a/ext/json/tests/json_encode_basic.phpt
+++ b/ext/json/tests/json_encode_basic.phpt
@@ -151,8 +151,8 @@ string(4) "null"
string(4) "null"
-- Iteration 26 --
-Warning: json_encode(): type is unsupported, encoded as null in %s on line %d
-string(4) "null"
+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"}"
===Done===
diff --git a/ext/json/tests/pass001.1.phpt b/ext/json/tests/pass001.1.phpt
index 7e15a7622a..a51f885780 100644
--- a/ext/json/tests/pass001.1.phpt
+++ b/ext/json/tests/pass001.1.phpt
@@ -90,10 +90,10 @@ $arr = json_decode($test, true);
var_dump($arr);
echo "ENCODE: FROM OBJECT\n";
-$obj_enc = json_encode($obj);
+$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR);
echo $obj_enc . "\n";
echo "ENCODE: FROM ARRAY\n";
-$arr_enc = json_encode($arr);
+$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR);
echo $arr_enc . "\n";
echo "DECODE AGAIN: AS OBJECT\n";
diff --git a/ext/json/tests/pass001.phpt b/ext/json/tests/pass001.phpt
index 43be11e2b0..1fd05fcdd8 100644
--- a/ext/json/tests/pass001.phpt
+++ b/ext/json/tests/pass001.phpt
@@ -79,10 +79,10 @@ $arr = json_decode($test, true);
var_dump($arr);
echo "ENCODE: FROM OBJECT\n";
-$obj_enc = json_encode($obj);
+$obj_enc = json_encode($obj, JSON_PARTIAL_OUTPUT_ON_ERROR);
echo $obj_enc . "\n";
echo "ENCODE: FROM ARRAY\n";
-$arr_enc = json_encode($arr);
+$arr_enc = json_encode($arr, JSON_PARTIAL_OUTPUT_ON_ERROR);
echo $arr_enc . "\n";
echo "DECODE AGAIN: AS OBJECT\n";
diff --git a/ext/json/tests/unsupported_type_error.phpt b/ext/json/tests/unsupported_type_error.phpt
new file mode 100644
index 0000000000..2564c6a3c8
--- /dev/null
+++ b/ext/json/tests/unsupported_type_error.phpt
@@ -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)