Skip to content
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

Deprecate the json_encode and json_decode functions #437

Open
soullivaneuh opened this issue Jan 16, 2024 · 1 comment
Open

Deprecate the json_encode and json_decode functions #437

soullivaneuh opened this issue Jan 16, 2024 · 1 comment

Comments

@soullivaneuh
Copy link

Summary

Since PHP 7.3.0, the json_encode and json_decode functions have a JSON_THROW_ON_ERROR to be set in order to throw an exception instead of returning false in case of error.

Also, Rector proposes to automatically adding it trough the official PHP upgrade migration rules.

Is is still interesting to maintain those function inside this project? If not, we may mark them as deprecated. Also, we should update the provided rector migration file accordingly:

safe/rector-migrate.php

Lines 461 to 462 in 3a6e43b

'json_decode' => 'Safe\json_decode',
'json_encode' => 'Safe\json_encode',

Any though?

@wjzijderveld
Copy link

wjzijderveld commented May 30, 2024

I do think it would be nice to keep this, as the flag isn't default so at the very least this creates a nicer interface than having to pass the flag every time. But that argument works better if you use Safe in a lot of places and are just used to having it around 😅

But I do think this library should account for it. See for example this snippet: https://3v4l.org/GX9mL
When using Safe while also passing JSON_THROW_ON_ERROR this method can create false exceptions, as json_last_error() will return the error from a previous call to json_decode where the flag was not passed.
We've had some flaky tests because of this, mostly noticeable when running tests in a random order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants