-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR #25490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
83d2f6d
to
d968567
Compare
Could you add some unit tests please? |
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); | ||
if (false === $decodedData) { | ||
$this->lastError = json_last_error(); | ||
throw new UnexpectedValueException( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should be on one line according to our CS policy
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
@@ -95,8 +95,11 @@ public function decode($data, $format, array $context = array()) | |||
$decodedData = json_decode($data, $associative, $recursionDepth); | |||
} | |||
|
|||
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { | |||
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); | |||
if (false === $decodedData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$decodedData
can be false without it being an error if you decode the "false"
string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, json_decode('false') return string 'false', not boolean false. false !== 'false'
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@diversantvlz you should try running it :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, like this?
@diversantvlz Could you please add some unit test ? |
@@ -95,8 +95,8 @@ public function decode($data, $format, array $context = array()) | |||
$decodedData = json_decode($data, $associative, $recursionDepth); | |||
} | |||
|
|||
if (JSON_ERROR_NONE !== $this->lastError = json_last_error()) { | |||
throw new UnexpectedValueException(JsonEncoder::getLastErrorMessage()); | |||
if (JSON_ERROR_NONE !== $this->lastError = json_last_error() and false === $decodedData) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please use &&
instead of and
(it doesn't have the same priority).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In fact using && will break precedence, but it should still be used, so that both operands should be swapped
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
Shouldn't we also check if the |
Options argument in json_encode is a bit mask. To understand if a flag is installed, additional calculations must be made. |
@diversantvlz Do you want me to help you adding a test ? |
@Simperfit Yes, why not. I would be thankful. I do not have enough time for this task, i can do it the next week. //for decode
json_decode('false') === false && json_last_error() === JSON_ERROR_NONE;
//for encode
$arr = array(
'utf8' => 'Hello World!',
'notUtf8' => "\xb0\xd0\xb5\xd0"
);
json_encode($arr) === false && json_last_error() === JSON_ERROR_UTF8;
json_encode($arr , JSON_PARTIAL_OUTPUT_ON_ERROR) === '{"utf8": "Hello World!", "notUtf8": null}' && json_last_error() === JSON_ERROR_UTF8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@diversantvlz FYI I pushed on your fork, with a test case, and reverting the change on JsonDecode since it cannot happen there (please provide a test case if you don't agree.)
@nicolas-grekas @dunglas Review test case please. |
3e75b6e
to
3fb8c58
Compare
Thanks @diversantvlz FYI, I reverted the change on JsonDecode because it just shouldn't change, partial jsons is only a feature of JsonEncode. |
Logic now fixed to work as expected on PHP 5.4 & 5.3. Status: reviewed |
Thank you @diversantvlz. |
…TIAL_OUTPUT_ON_ERROR (diversantvlz) This PR was merged into the 2.7 branch. Discussion ---------- [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR | Q | A | ------------- | --- | Branch? | 2.7 | Bug fix? | yes | New feature? | no <!-- don't forget to update src/**/CHANGELOG.md files --> | BC breaks? | no | Deprecations? | no <!-- don't forget to update UPGRADE-*.md files --> | Tests pass? | yes | Fixed tickets | no | License | MIT | Doc PR | no <!-- - Bug fixes must be submitted against the lowest branch where they apply (lowest branches are regularly merged to upper ones so they get the fixes too). - Features and deprecations must be submitted against the master branch. - Replace this comment by a description of what your PR is solving. --> Php function json_encode/decode with option JSON_PARTIAL_OUTPUT_ON_ERROR return result on error, but if have is error json_last_error() always return error code even if there is a result and it is not false. Because of this is impossible set JSON_PARTIAL_OUTPUT_ON_ERROR option across variable $context. Current fix solves this problem. Verification on the false is completely correct, since json_encode / decode returns false only on error if not set JSON_PARTIAL_OUTPUT_ON_ERROR option. Such have a problem e.g when encoding data is not utf-8 (emoji from facebook). Commits ------- e7e410b [Serializer] Fixed throwing exception with option JSON_PARTIAL_OUTPUT_ON_ERROR
Php function json_encode/decode with option JSON_PARTIAL_OUTPUT_ON_ERROR return result on error, but if have is error json_last_error() always return error code even if there is a result and it is not false. Because of this is impossible set JSON_PARTIAL_OUTPUT_ON_ERROR option across variable $context.
Current fix solves this problem.
Verification on the false is completely correct, since json_encode / decode returns false only on error if not set JSON_PARTIAL_OUTPUT_ON_ERROR option.
Such have a problem e.g when encoding data is not utf-8 (emoji from facebook).