yii2: Error return when using JSON_FORMAT with NULL value
Invoke return $this->asJson(null); in controller
or
$response->format = Response::FORMAT_JSON; $response->data = null;
will output nothing.
In standard json, the null value must be output a ‘null’ string. But when I set output format to json, and value is null, it output nothing. I think the right output is a ‘null’ string, hope fixed.
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (12 by maintainers)
Commits related to this issue
- Fixes #14759: Fixed `yii\web\JsonResponseFormatter` output for `null` data — committed to yiisoft/yii2 by misantron 6 years ago
- PHPUnit test for pull request #14759 — committed to h0mjam/yii2 by deleted user 5 years ago
- added test case for checking BC of #16772 and #14759 — committed to yiisoft/yii2 by cebe 5 years ago
@cebe I don’t think we should modify
contentdirectly inController::asJson(). The better solution is to changeJsonResponseFormatterbehavior like this:Might this not be a breaking change?
Strict correctness of the JSON encoding isn’t necessarily the requirement if Yii, according to its documentation, specifies that a PHP
nullmeans “don’t output anything”. People might be using it.@cebe I think that use case is irrelevant here - empty string is invalid JSON. “null response” should return
nullencoded as JSON.https://3v4l.org/R4mRJ
Please do.
I’m ready to make PR with this change and updated tests