Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:105671 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 5205 invoked from network); 11 May 2019 05:06:53 -0000 Received: from unknown (HELO mail-vs1-f46.google.com) (209.85.217.46) by pb1.pair.com with SMTP; 11 May 2019 05:06:53 -0000 Received: by mail-vs1-f46.google.com with SMTP id j184so4726180vsd.11 for ; Fri, 10 May 2019 19:11:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:reply-to :from:date:message-id:subject:to:cc; bh=SqV+c5sKTCNgfTWMKgDCFru+WXWVcSoNOPrAkXIEEdw=; b=BmVwz364Ri9SUkbOxgcHPlLkoz+mzUq8C2EDXF7U5KInS1bf2iXE8WRqdsu4ucvDJf 0V/cBeZTUWTtm9/ais1eD3qm3OCIkUYuT4wvHF7DmVJum5nngWKD/vpoDOGojBn3AGa7 Q1/EimNT1xMzXeZA8ttYvCX6ieuRWyt/BvXNjL0PwWtUimMGZtRlrLbbG/9dla6cSVN2 PaQBzVu4sL+2YykzZ8k3f+S6YXykUhUN9tIS3UMWGc5VXZX7Kw0anlsIqick4gBFVJMN inaiLLEftWQ5tgECTikAZ7lZQHvLHJN0AVLasE7gqCQLjtAexvZhiinawArmGrNCLJaZ 3xcg== X-Gm-Message-State: APjAAAWbPBsKMJBSrMl3X8qqpo8h0v1XoRQUBQxab4Ap2phJjo1krlOb qYv4cWRFR0Ntf8XUf9Cv3rQD+MT5ytLfLFfcXPk6ZidpZDk= X-Google-Smtp-Source: APXvYqwXDc5g1fTluUawqAjYJ6h21ejadXxx5yx4BATBK5FRDfrAjS7uQ1JGVMKJM59fGuUKz6AYnzyR3vxQtSuAg7s= X-Received: by 2002:a67:f507:: with SMTP id u7mr7666203vsn.158.1557540700349; Fri, 10 May 2019 19:11:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Reply-To: bishop@php.net Date: Fri, 10 May 2019 22:11:17 -0400 Message-ID: To: =?UTF-8?Q?Riikka_Kalliom=C3=A4ki?= Cc: PHP internals Content-Type: multipart/alternative; boundary="0000000000002aea130588933482" Subject: Re: [PHP-DEV] JSON_THROW_ON_ERROR implementation detail From: bishop@php.net (Bishop Bettini) --0000000000002aea130588933482 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Fri, May 10, 2019 at 2:52 PM Riikka Kalliom=C3=A4ki < riikka.kalliomaki@riimu.net> wrote: > > The new code has what looks like, to me, a refactor bug. I'd expect tha= t, > when one adds the JSON_THROW_ON_ERROR flag, one must also remove the > subsequent json_last_error() handling, as it's no longer relevant to that > call and instead relevant to the most recent prior call. > > This does create a kind of an unfortunate situation with vendor > libraries, however. Any library that accepts arbitrary json flags and > uses json_last_error() for error handling will be broken if > JSON_THROW_ON_ERROR is passed as a flag. > I would expect such libraries to be sensitive to the possible variations implied by accepting arbitrary arguments, and would compensate accordingly. For example: function library_json_decode(string $json, int $options) { if (70300 <=3D PHP_VERSION_ID) { $options &=3D ~JSON_THROW_ON_ERROR; } $decoded =3D json_decode($json, $options); if (JSON_ERROR_NONE !=3D=3D json_last_error()) { throw new LibraryException(json_last_error_msg(), json_last_error()); } return $decoded; } This means that library consumers effectively may have to become aware > of how a library internally deals with json errors. > > It kind of turns JSON_THROW_ON_ERROR into unconventional BC break, > because any code that takes arbitrary flags and uses json_last_error() > needs to account for it. In some cases, I imagine, it can be necessary > to add a "json_encode(null);" before the actual json handling just to > reset the error state, just in case. > I agree this is possible, but I contend any function taking arbitrary options needs to understand the implication of those options. It's in many ways just like any other user-supplied data: authors must understand the possible values available for the supported versions and handle the accordingly. > I wouldn't be surprised if this becomes a source of unexpected bugs in > json handling code down the line. In my opinion, it's very unexpected > behaviour that json_last_error() can reflect the state prior to the > previous json_encode/json_decode call depending on the flags passed to > the function as this has not been the case before. > > Perhaps it should be documented in the BC breaks section for 7.3 that > json_last_error() may now reflect the state of arbitrary previous > call? > Yes, better documentation surrounding JSON_THROW_ON_ERROR would be welcomed. The docs are, right now, quite lean. --0000000000002aea130588933482--