On Fri, Aug 26, 2022, at 1:41 PM, Ayesh Karunaratne wrote: >> whether returning boolean is the right thing to do at all. It seems obviously >> intuitive it should, returning true for valid and false for invalid JSON >> but then if you consider you're still going to be in the situation of >> calling json_last_error() if you want to know why invalid JSON was invalid >> and in particular you might not expect the "last error" to have changed >> just from an attempt to check a string. How can there be an error when by >> definition you weren't trying to do anything except check the validity of >> some unknown data? Not sure what the answer is there...curious what other >> people's views are on that. I don't think throwing an exception on invalid >> JSON is the right answer in any case. > > One of the reasons why I like `json_validate` over `is_json` is that > the former does not imply a boolean return value as strongly as the > latter. > > I think we should not modify any state with the `json_validate` > function, and I'd be happy with the function returning 0 for a valid > JSON, or the error code as an integer (`JSON_ERROR_STATE_MISMATCH`, > `JSON_ERROR_SYNTAX`, etc.). `JSON_ERROR_NONE` constant is already > assigned 0, so they align quite well too.
The problem here is that you're using a falsy return (0) to indicate "it validated", which is logically true. PHP's type juggling makes that a very bad idea. I somehow doubt it will get much traction, but this is the sort of case where a "naked either" approach could work well: https://peakd.com/hive-168588/@crell/much-ado-about-null Though there's also the risk then of all objects being "truthy", since there seems to be no appetite for a __toBool() method as that's been shot down before. At some point, we need to realize that our current design philosophy boxes us in on too many sides and something is going to have to change. I see the argument for json_last_error() not being a great fit, but given the limitations we've chosen to have in the language it seems like "return bool and use json_last_error()" is the least-bad option. --Larry Garfield -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php