php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #81524 json_encode() on JsonSerializable populates the properties HashTable
Submitted: 2021-10-13 22:54 UTC Modified: -
From: tstarling@php.net Assigned:
Status: Open Package: JSON related
PHP Version: 8.1Git-2021-10-13 (Git) OS:
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please — but make sure to vote on the bug!
Your email address:
MUST BE VALID
Solve the problem:
7 + 8 = ?
Subscribe to this entry?

 
 [2021-10-13 22:54 UTC] tstarling@php.net
Description:
------------
I noticed that if you do json_encode() on a JsonSerializable object, it always populates the properties hashtable, because php_json_encode_serializable_object() needs a hashtable to have somewhere to set the recursion flag.

You might think it could just set the recursion flag on the object, but a few test cases (and probably users) do var_dump($this) in jsonSerialize(), which breaks if you protect the object prior to calling jsonSerialize().

It's possible to solve this by adding a second recursion flag to gc_flags, but that's a bit of a joke solution since I wouldn't expect a PR which consumes a bit in gc_flags to be accepted. But here it is if you're curious: 
https://github.com/tstarling/php-src/commit/1e975dfa731ceba4533aea0efdbd803d226963c9


Patches

Add a Patch

Pull Requests

Add a Pull Request

 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun May 28 13:03:46 2023 UTC