Improve json_encode error handling
authorNikita Popov <nikic@php.net>
Sat, 23 Jun 2012 18:46:27 +0000 (20:46 +0200)
committerNikita Popov <nikic@php.net>
Sat, 23 Jun 2012 18:51:52 +0000 (20:51 +0200)
commit84fe2cc890e49f40bac7c3ba74b3cfc6dc4cef2f
tree4b07a64554232483751acac245a9eb43154d0ecc
parentcc90ac54beb7359e5a3210261ce09159bbc43e92
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.
ext/json/JSON_parser.h
ext/json/json.c
ext/json/tests/003.phpt
ext/json/tests/004.phpt
ext/json/tests/inf_nan_error.phpt [new file with mode: 0644]
ext/json/tests/json_encode_basic.phpt
ext/json/tests/pass001.1.phpt
ext/json/tests/pass001.phpt
ext/json/tests/unsupported_type_error.phpt [new file with mode: 0644]