Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:100070 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 45708 invoked from network); 28 Jul 2017 10:04:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Jul 2017 10:04:10 -0000 Authentication-Results: pb1.pair.com smtp.mail=ryan.jentzsch@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=ryan.jentzsch@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.52 as permitted sender) X-PHP-List-Original-Sender: ryan.jentzsch@gmail.com X-Host-Fingerprint: 209.85.214.52 mail-it0-f52.google.com Received: from [209.85.214.52] ([209.85.214.52:36274] helo=mail-it0-f52.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id FD/80-40376-91C0B795 for ; Fri, 28 Jul 2017 06:04:10 -0400 Received: by mail-it0-f52.google.com with SMTP id v205so78518305itf.1 for ; Fri, 28 Jul 2017 03:04:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=ifPl/ocIY+0khbAgiRSzCBtaMbmAUlyape/2gieLuSM=; b=VedqBZbI2hiV88OpUkoXEcw/tgLWTRWLfC6sFPk++6DCpq0p+hVKsboEu2cd9jnasS 3drK2j0mu949wqbMFP6/Ht/xsejmYQBJCqnpBBiLWbO/UZreoXD8oMPF7plLmimUeUkg C3trEoWspt1WFew8KzuIyEvgh0UWqpRORE3T6+0qBoFwq6drnwW99EUuXee8Z2uB04+Z QPWwkfHuU4mSD4G3YoaBU5L52l39/neE63lcrr4c45XzK3GhJNyoFpNo2sxYki8J68iY eDqtwNSn+S/OkEoZazQrglqitReGb37VUWigMaWmBFXh7PBri7AXuRELNGkKeBWe1b+G isjw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=ifPl/ocIY+0khbAgiRSzCBtaMbmAUlyape/2gieLuSM=; b=n8v6RqsQBaOmn7kr+yOQ6LVEOcGTGFPFBuXMmiBujrNQoWroLgA5bHOTAXNnU22BW5 ry1YF9L0TEMnW+pQLjfhaFxOClAKTIpbeU+OGgd6LYjjNJeNmnr7qI0dxsxFEjXN1vpY KalFb1624XqyHpfe4CIzlgjPreJ6HEubvp1vj3lAGMBttiu3IB388G1saBIFhH12jQhZ A7BsT8paOGltBaLeQtxzcIrUYkEM94TjnsGIvgT+xeNixvzwh05xF+H9k1YxzMOhVb9r dEuFIuVuMK2k6eZ+ch1NOQAxvUi/UorDNyQGPFXcpDM6ztD07fDeGW9ZR23XVK9mK+il jJCA== X-Gm-Message-State: AIVw110gNlfBKdgeweds45mMNVoY++tDkDveWAhqrUVxXOPjuaSf71W1 C8w5/5L+JcZ7Bf9SEEo7EjYcKN3Zsg== X-Received: by 10.36.252.69 with SMTP id b66mr8583359ith.97.1501236247106; Fri, 28 Jul 2017 03:04:07 -0700 (PDT) MIME-Version: 1.0 Received: by 10.2.46.66 with HTTP; Fri, 28 Jul 2017 03:03:36 -0700 (PDT) In-Reply-To: References: Date: Fri, 28 Jul 2017 04:03:36 -0600 Message-ID: To: Niklas Keller Cc: Giovanni Giacobbi , Craig Duncan , Internals Content-Type: multipart/alternative; boundary="94eb2c0b0ccc3b98a305555dcd82" Subject: Re: [PHP-DEV] json_encode() / json_decode() warnings From: ryan.jentzsch@gmail.com (Ryan Jentzsch) --94eb2c0b0ccc3b98a305555dcd82 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable I can't count how many times I've been bitten by this. From the infamous fractal blog: "Parts of PHP are practically designed to produce buggy code. json_decode returns null for invalid input, even though null is also a perfectly valid object for JSON to decode to=E2=80=94this function is compl= etely unreliable unless you also call json_last_error every time you use it." Most functions in PHP return false as an indicator for an invalid call. json_decode() returns null -- changing this to return false is also a breaking change that may not survive a vote. Because of the unreliability I don't use this function and always rely on 3rd party JSON libraries instead. On Fri, Jul 28, 2017 at 12:59 AM, Niklas Keller wrote: > 2017-07-28 8:56 GMT+02:00 Giovanni Giacobbi : > > > On 27 July 2017 at 18:00, Craig Duncan wrote: > > > >> On 27 July 2017 at 16:57, Niklas Keller 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 =3D null) { > > if ($json =3D=3D "") > > return null; > > > > $retval =3D json_decode($json, true, 512, JSON_BIGINT_AS_STRING); > > if (json_last_error() !=3D 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 > --94eb2c0b0ccc3b98a305555dcd82--