Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:118537 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 37458 invoked from network); 29 Aug 2022 09:43:08 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 29 Aug 2022 09:43:08 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 255CC1804AB for ; Mon, 29 Aug 2022 02:43:08 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f178.google.com (mail-vk1-f178.google.com [209.85.221.178]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Mon, 29 Aug 2022 02:43:07 -0700 (PDT) Received: by mail-vk1-f178.google.com with SMTP id g185so3465875vkb.13 for ; Mon, 29 Aug 2022 02:43:07 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc; bh=7RZYgr6dNUhnaHvuTTutQ+3CElPjuAe8CIy1anvkjDQ=; b=LPgkbz+8Tt1rcCgQWad5ug5TBW3B45W/a7mgkuUAEHTWAkIRj++8D13RwKKn/Yau9c k0WH0Dp2/n48vspc//LcqGMB1VJ9esJQp+6moCr6Yf+8FE3UcCki9NQrqFoN7QCs8CLW Ev7JVcIw1P4Bf7CvQb7DJ91SUh9933s3JDJsQab1s2xIeJeHj+ff5u1c+L1wwdiR7i4J YpsDhEO2da2hXXJlSxi1br9pBc+1xyQsvCHyZaxC/dI6b8Obs2+te6Ibf21JG2PGlKR7 orCbOLyilaNlixkeYd1PwjqOz7jxgwbG2J0m4RfnNmdRohuWX0CQuWXBNQNtUx4Vez13 cM4A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=7RZYgr6dNUhnaHvuTTutQ+3CElPjuAe8CIy1anvkjDQ=; b=7YYY7T0t8YPBeDFVu8njBtAyIrCf0PMmBTWeSK0HOVVyQHzApgzDRbn3nRhCex69SW 3O8j8VSMw+8FKnJCpF0ddLmUjtc5k/cbH/Rs+DlSYvnUVQpRHyYg+aZHLC8NyxXnHJI0 N9miD4KtF2wy4IJiIhInqzRKeERsxtyfBPGjDCnXvlVKwS4bSaUUIPSwVfQUZR7Od+iz LIyAXjug5b1NklTp+RCnUWRqmHlKtfjP9iURJ9J+BLy3ly6BvjsNUgODcZFOg/ozU/Fv j9oLMZeVIl1TqbH4wzYnHmcVijOsXqdcK2N1plnCbR+Rys/KeTUS07enlRx9NUtcsnF8 Qntg== X-Gm-Message-State: ACgBeo3kkDaG76e/bodRqHPb4bnaOmi+cN94FYcXzo+OlOJSX4xjzuJT 1uTBHtcx3egYnXpTw2vkV4sM99wLeHATSh3uFk0= X-Google-Smtp-Source: AA6agR6zoT1m0jEec9B0msW1lOvNGJMh0xmBF7mTVkAxzeLUKsvq0RBSkFDtJqxoeQR4aTN/I3bzUeC0yts0lFcq5xo= X-Received: by 2002:a1f:a74c:0:b0:379:1113:4f07 with SMTP id q73-20020a1fa74c000000b0037911134f07mr2802026vke.40.1661766187085; Mon, 29 Aug 2022 02:43:07 -0700 (PDT) MIME-Version: 1.0 References: <8D53AD5B-7CFC-4820-9EE4-FEB365D327A8@woofle.net> In-Reply-To: Date: Mon, 29 Aug 2022 11:42:56 +0200 Message-ID: To: Deleu , PHP Internals List Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC json_validate() - status: Under Discussion From: dev.juan.morales@gmail.com (juan carlos morales) El lun, 29 ago 2022 a las 11:26, Deleu () escribi=C3=B3= : > > > > On Mon, Aug 29, 2022 at 11:19 AM juan carlos morales wrote: >> >> El lun, 29 ago 2022 a las 11:06, Deleu () escribi=C3= =B3: >> > >> > Has the option of returning a Result object been discussed/considered?= Can it be an option? I imagine that if `json_validate(): JsonValidationRes= ult` always returns a `JsonValidationResult` which contains a `public reado= nly 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 o= n 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 st= ring 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 =3D '{}'; > > $result =3D 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