Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125434 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 48B691A00BD for ; Thu, 5 Sep 2024 08:56:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725526695; bh=bVQAUKMxbim/lk8JBJw/LUAm4bnyNasEz6rU+ymDbtw=; h=References:In-Reply-To:From:Date:Subject:To:Cc:From; b=j5t8ZhRwJkbTMKv7Kncbu5jJrXvxW0DT030wrcNCij9xYimJ3W12l8cNgxck0NWEv fVZeEqYHWFFE/PteE8BqU5hnocSc5jnqaRsdEXYzaDAch9WLaaGl5L3Emmr1YTOr27 6dfzeVm9KOngPRjteJKREqo3/Bta4GMMf0BkFJtN5FssZWlk5s5clv9DFcvs5xo+yF PkvqsXbVf/M/12+IeSK2YakkdlkwRhrVRGrDtWCuPJ4JDiCK2Tb20teATDFkjPvLXL DhNqGBnwAt2ewUwiZv1cRe1soHAgqLZW0jNdmRIEdOpFl1FqUnfZXcHSvdfxq9WFMv 8Xmt+sS6fm9YQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id A575618006E for ; Thu, 5 Sep 2024 08:58:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, HTML_MESSAGE,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE, SPF_PASS autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Thu, 5 Sep 2024 08:58:14 +0000 (UTC) Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-e02b79c6f21so636167276.2 for ; Thu, 05 Sep 2024 01:56:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1725526575; x=1726131375; darn=lists.php.net; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=bVQAUKMxbim/lk8JBJw/LUAm4bnyNasEz6rU+ymDbtw=; b=QWKqZAKVslIJ7oCLcFtiWxR2sUdlmlm8uCpKrN0DxoMSxPAEc01KlZTaJAh2RlkEph VjoXbRVofLIRSX/L5TFb7ubK3pxVCo7wVB7moq9ZatfSz9i9SzSP/qr3hMtFkEFDzr3x 3/CFdWxgAjOULqS73H6RAONGP5Vp5bWB/pQcz69BuHrvdWL7qEGAHfx5H2Aqp18MGvpM yx5t5Fx1UtS6dKaNGf/rU+AeRwfB4qn8XQTJUG5rfYxmvTelz9MZP3f2LhdkbvLQLS2c /nGY9xN6fnYRZJuFTjvX637Fw7IGG4pdAVB4/2WPHDl1lEOYSKHfmaES+W3Y6PfG0cPM cWhw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725526575; x=1726131375; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=bVQAUKMxbim/lk8JBJw/LUAm4bnyNasEz6rU+ymDbtw=; b=HhMFNa+8xmrHuwv/OrSJPNBE+pD7qeVogAzITxYJ4PhcwLGcchLQqFrr+2eKBtws7S UAO2MU4WExqKx1I/1mh7iyhhANzCyVTy3Y9u7JhVAHw+cyJn+YHkJcPVZPOdFK3iCAFQ ncJ3fBqO6Wkd8id6mWFs5o7xxpAjUCv/d86Ny28sbHB/ZMnfozuFcMYKtc1XPMRedsGM x/5MCxfWpfg3KwtiYsW/bW9Pfpk7nZ7XFRUJYMQVmh02SNBNmmspz+ucCwqJYDyAugc/ 1p9U/+XUCelpSoZWu6JmWHek1zXkg6wiq2zjEpNsW2T8LSIrHO0BzXQ8VVSVtmcGFIfo ttlQ== X-Gm-Message-State: AOJu0YwiWq0jxPl6PC9Y8ExisobJQyJXKa3saLY9K9pir+/WT8nznD7q z/tKFVvdGDxGSlvSQjeC2R2nyfa6j77fTzoHcWlV7NLCVZF1ngOYix0or1LpvYuY3Ln5r8H855j DFobD8AAOuNPaQTbwh68wOVuG6q1hyCXAbOY= X-Google-Smtp-Source: AGHT+IEc+QG+0CcH/ZSnVTNYz7/weJ3gQZpiYXDdAJEeeIRvvqz49wpCIXTKJbNBSuKEcOALEIXmQ6FPP/NnfIEKR5U= X-Received: by 2002:a05:6902:f8a:b0:e13:ca39:f80d with SMTP id 3f1490d57ef6-e1a7a1c2addmr23542766276.38.1725526575290; Thu, 05 Sep 2024 01:56:15 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow MIME-Version: 1.0 References: In-Reply-To: Date: Thu, 5 Sep 2024 11:55:58 +0300 Message-ID: Subject: Re: [PHP-DEV] RFC: Deprecate json_encode() on classes marked as non-serializable To: Philip Hofstetter Cc: PHP internals Content-Type: multipart/alternative; boundary="00000000000092251f06215b769d" From: drealecs@gmail.com (=?UTF-8?Q?Alexandru_P=C4=83tr=C4=83nescu?=) --00000000000092251f06215b769d Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Tue, Sep 3, 2024 at 2:27=E2=80=AFPM Philip Hofstetter wrote: > Hello, > > As per my previous email to the list, I have now created the official RFC > to deprecate calling json_serialize() on instances of classes marked with > ZEND_ACC_NOT_SERIALIZABLE. > > https://wiki.php.net/rfc/deprecate-json_encode-nonserializable > > > Sharing my experience, I never use json_encode on objects but on arrays (that are both JSON objects or JSON arrays). When I see an object implementing JsonSerializable, I think it is the wrong approach, and I am usually able to find a better way. Maybe if we could go back in time, we would not allow json_encode on an object, except if it implemented JsonSerializable, but that ship sailed long ago. To your proposal, I think the BC break would be pretty big and I don't see a way it would pass. On https://www.php.net/json_encode we can read: > If a value to be serialized is an object, then by default only publicly visible properties will be included. So that's the expected behaviour. Yes, you can say that encoding as JSON is just "another serialization method", but the default method in PHP, using json_encode()/json_decode(), is not symmetrical when using objects. And here lies the difference with serialize()/unserialize(), as this one aims to be symmetrical, and where it can't be, it has a way to stop the serialization. I am happy with the current way it works, getting an empty JSON object if there are no public properties for a Generator or Closure. And I don't think having an error for them would improve the language in a meaningful way, and the BC break is not worth it. Regards, Alex --00000000000092251f06215b769d Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

On Tue, Sep 3, 2024 at 2:27=E2=80=AFPM Ph= ilip Hofstetter <phofstett= er@sensational.ch> wrote:
Hello,

As per my previous email to the list, I have now= created the official RFC to deprecate calling json_serialize() on instance= s of classes marked with ZEND_ACC_NOT_SERIALIZABLE.
<= br>



Sharing my experience, I never use json_encode on objects but on arra= ys (that are both JSON objects or JSON arrays).
When I see an obj= ect implementing JsonSerializable, I think it is the=C2=A0wrong approach,= =C2=A0and I am usually able to find a better way.
Maybe if we cou= ld go back in time, we would not allow json_encode on an object, except if = it implemented JsonSerializable, but that ship sailed long ago.

To your proposal, I think the BC break would be pretty bi= g and I don't see a way it would pass.
On=C2=A0https://www.php.net/json_encode we can re= ad:
>=C2=A0If a value to be serialized is an object, then by d= efault only publicly visible properties will be included.
So that= 's the expected behaviour.

Yes, you can say th= at encoding as JSON is just "another=C2=A0serialization method", = but the default method in PHP, using json_encode()/json_decode(), is not sy= mmetrical=C2=A0when using objects.
And here lies the difference w= ith serialize()/unserialize(), as this one aims to be symmetrical, and wher= e it can't be, it has a way to stop the serialization.

I am happy with the current way it works, getting an empty JSON ob= ject if there are no public properties for a Generator or Closure.
And I don't think having an error for them would improve the language= in a meaningful way, and the BC break is not worth it.

Regards,
Alex

--00000000000092251f06215b769d--