![]() JsonException would be a new class that subclasses Exception. JSON_PARTIAL_OUTPUT_ON_ERROR would override and disable JSON_THROW_ON_ERROR, so that generic wrapper functions that always pass the JSON_PARTIAL_OUTPUT_ON_ERROR flag continue to support JSON_PARTIAL_OUTPUT_ON_ERROR. The global error state is left untouched, and if an error occurs that would otherwise set it, these functions instead throw a JsonException with the message and code set to whatever json_last_error ( ) and json_last_error_msg ( ) would otherwise be respectively. When passed this flag, the error behaviour of these functions is changed. This RFC instead proposes adding a new option flag value for json_decode ( ) and json_encode ( ), JSON_THROW_ON_ERROR. However, to immediately change the default behaviour of these functions to throw would be a significant backwards-compatibility issue, and producing a notice or warning is not ideal for predictable, routine errors that are not bugs or poor code style (as JSON decoding may be when given user input, for instance). This situation is suboptimal, and throwing exceptions would be cleaner: there would be no confusion between error values and correct results, no (possibly outdated) global state, no program execution after the error is thrown unless explicitly handled, and error messages would be neatly traceable to their source. But both functions also do not halt program execution by default on error, or even throw a warning. json_encode ( ) also uses this system, but at least does have a clear error return value. It is only possible to know if an error occurred by calling json_last_error ( ) or json_last_error_msg ( ), which return the global error state in machine-readable and human-readable forms respectively. json_decode ( ) returns null upon erroring, but null is also a possible valid result (if decoding the JSON “null”). Unfortunately, both have suboptimal error handling. PHP has two functions for dealing with JSON, json_decode ( ) and json_encode ( ).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |