Re: RFC json_validate() - status: Under Discussion
| From: | juan carlos morales | Date: | Mon, 29 Aug 2022 09:42:56 +0000 |
| Subject: | Re: RFC json_validate() - status: Under Discussion | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-118537@lists.php.net to get a copy of this message | ||
El lun, 29 ago 2022 a las 11:26, Deleu (<deleugyn@gmail.com>) escribió:
>
>
>
> On Mon, Aug 29, 2022 at 11:19 AM juan carlos morales <dev.juan.morales@gmail.com> wrote:
>>
>> El lun, 29 ago 2022 a las 11:06, Deleu (<deleugyn@gmail.com>) escribió:
>> >
>> > Has the option of returning a Result object been discussed/considered? Can it be an
>> > option? I imagine that if
json_validate(): JsonValidationResult
>> > always returns a JsonValidationResult which contains a
>> > public readonly bool $valid and a public readonly
>> > ?string $error it would be better than both options on the table right now. The
>> > option of returning CLI-like results means that we will need a if (!
>> > json_validate()) to treat a valid JSON (really awkward) and the option of using
>> > json_last_error() relies on an internal state instead of an
>> > immutable structure. Effectively, what we need is to return a complex structure which can contain a
>> > boolean and a string and that is a class/object.
>> >
>> Interesting, it was not considered.
>>
>> I still think BOOLEAN is my preferred choice, but can you provide an
>> example about how the code would look like with your approach? Imagine
>> you have the feature like in your suggestion ... how a developer would
>> write the code?
>
>
> ```
> $json = '{}';
>
> $result = json_validate($json);
>
> if (! $result->valid) {
> throw new \Exception('Invalid JSON provided: ' . $result->error);
> }
>
> $something->saveUserProvidedJson($json);
> ```
>
> --
> Marco Deleu
Thanks for participating on the discussion and I will take note about
this also, but ...
Quick question: Is in PHP some rule, norma, estandard about "result
objects" already? (like, "should have these methods, and implement
this interface, etc.)
FYI: Just want to clarify that there is nothing bad/wrong about
relying on "internal state" by calling json_last_error(), as this
pattern is used not only by json_decode() but also mysqli extension
for example, among others. Nothing wrong honeslty IMO.
RFC: https://wiki.php.net/rfc/json_validate
Implementation: https://github.com/php/php-src/pull/9399