if you pass an empty value in the json_decode(), it will return null
var_dump(json_decode(""));
//null
but the PhalconHelperJson::decode("") throws out Exception:
var_dump(\Phalcon\Helper\Json::decode(''))
//InvalidArgumentException: json_decode error: Syntax error in BaseController.php:34 Stack trace: #0 BaseController.php(34): Phalcon\Helper\Json::decode() # ...
as a result, early compatibility breaks $ this->request->getJsonRawBody()
What behavior will be correct?)
Thx
This is because the Phalcon uses the JSON_THROW_ON_ERROR constant in json_decode options
I don't know how good or bad it is.
But in my code I always use this constant
JSON_THROW_ON_ERROR
JSON_THROW_ON_ERROR in the json_decode() is known.
But in the phalcon, there is a problem with getJsonRawBody(), there we can not specify the json decode options.
Nonetheless , by default, I think, the behavior of the json_decode and the PhalconHelperJson::decode() should be the same.
JSON_THROW_ON_ERROR
JSON_THROW_ON_ERROR in the json_decode() is known.
But in the phalcon, there is a problem with getJsonRawBody(), there we can not specify the json decode options.
Nonetheless , by default, I think, the behavior of the json_decode and the PhalconHelperJson::decode() should be the same.
As temporarily fix, you can override this method
Well, even in plain PHP it is error:
var_dump(json_decode(""));
echo json_last_error_msg();
Syntax error
Well, even in plain PHP it is error:
var_dump(json_decode("")); echo json_last_error_msg();Syntax error
but the result of json_decode("") will still be null if you do not call json_last_error_msg()
Let me tell you what caused me to write Issue.
In the project on Phalcon 3, there is a class that accepts $this->request->getJsonRawBody(), and with an empty request it worked out normally, it might be worthwhile to do validation on the input data, but for now let's say that it doesn鈥檛 matter.
So with the upgrade to Phalcon 4, the behavior has changed, now an error is issued.
i.e now just calling $this->request->getJsonRawBody() causes an error.
I temporarily replaced $this->request->getJsonRawBody() with聽 I am using json_decode($this->request->getRawBody())
and it works :) but in essence - it should work the same
P.S. I seem not the only one https://forum.phalcon.io/discussion/20587/getjsonrawbody-strange-behavior
Fixed in 4.0.x branch. Thank you for the bug report, and for helping us make Phalcon better.
Thanks guys!
Most helpful comment
Fixed in 4.0.x branch. Thank you for the bug report, and for helping us make Phalcon better.