Re: json_encode() / json_decode() warnings
| From: | Niklas Keller | Date: | Fri, 28 Jul 2017 06:59:54 +0000 |
| Subject: | Re: json_encode() / json_decode() warnings | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-100069@lists.php.net to get a copy of this message | ||
2017-07-28 8:56 GMT+02:00 Giovanni Giacobbi <giovanni@giacobbi.net>:
> On 27 July 2017 at 18:00, Craig Duncan <php@duncanc.co.uk> wrote:
>
>> On 27 July 2017 at 16:57, Niklas Keller <me@kelunik.com> wrote:
>>
>> > It should rather just throw exceptions. Warnings do not really allow
>> error
>> > handling, they just allow error reporting.
>> >
>> >
>> Agreed, but I can't see Exceptions passing the vote. Warnings can be
>> silenced by those that don't care and converted to Exceptions by those
>> that
>> do
>>
>
> Error management is a painful topic in PHP, expecially when considering to
> the way fopen() behaves, where you *need* to use the "@" suppression if
> you want it to behave the way you expect it to behave.
>
> About Exceptions you can easily build a framework around core functions,
> for example this is in my core library for all projects:
>
> ```php
> function safe_json_decode($json = null) {
> if ($json == "")
> return null;
>
> $retval = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);
> if (json_last_error() != JSON_ERROR_NONE)
> throw new JsonDecodeException(json_last_error_msg(),
> json_last_error());
>
> return $retval;
> }
> ```
>
> So yes, the behaviour of json_decode() might not be optimal, but it's fine
> the way it is.
>
Yes, I know. There's https://github.com/DaveRandom/ExceptionalJSON.
While the current API works, I'm not sure whether I'd say its fine.
Regards, Niklas