rfc:deprecate-json_encode-nonserializable

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
rfc:deprecate-json_encode-nonserializable [2024/09/05 08:44] pilifrfc:deprecate-json_encode-nonserializable [2024/09/05 10:00] (current) – more detailed analysis of affected classes. Info about workaround for classes with public fields. pilif
Line 1: Line 1:
 ====== PHP RFC: Deprecate json_encode() on classes marked as non-serializable ====== ====== PHP RFC: Deprecate json_encode() on classes marked as non-serializable ======
-  * Version: 0.10+  * Version: 0.11
   * Date: 2024-09-05   * Date: 2024-09-05
   * Author: Philip Hofstetter, phofstetter@sensational.ch   * Author: Philip Hofstetter, phofstetter@sensational.ch
Line 45: Line 45:
 At the time of writing this RFC, the following list of classes (and their subclasses) are affected by this RFC  and calling <php>json_encode()</php> on them will throw a deprecation warning in the future. At the time of writing this RFC, the following list of classes (and their subclasses) are affected by this RFC  and calling <php>json_encode()</php> on them will throw a deprecation warning in the future.
  
-Most of them are currently json-encoded as ''%%{}%%'', no matter what they represent internally (some of theselike <php>PDOStatement</php> have public properties which are present in the output of <php>json_encode()</php>).+Some of those are containers of a sort, where this encoding is especially misleading (aside of <php>Generator</php> which was the motivator for this RFC, <php>WeakMap</php> stands out specifically). 
 + 
 +Most of the non-serializable classes have no public properties and thus encode as ''%%{}%%''
 + 
 +Those which currently do have public properties are still mostly meant for internal usage and thus not ideal candidates to <php>json_encode()</php> them. However, if code wants to explicitly turn any such instances into JSON in light of the deprecation currently proposedcasting such instances into <php>array</php> before JSON encoding them is a valid workaround. 
 + 
 +<code php> 
 +$a = new SimpleXmlElement('<a><b>3</b><c>foo</c></a>'); 
 +echo json_encode($a); // {"b":"3","c":"foo"}, with deprecation warning 
 +echo json_enode((array) $a); // {"b":"3","c":"foo"}, no deprecation warning 
 +</code> 
 + 
 +=== Classes with public fields appearing in json_encode(output === 
 +  * CURLFile (has three public properties) 
 +  * PDORow (has one public property, queryString) 
 +  * PDOStatement (has one public property, queryString) 
 +  * SimpleXMLElement (might be useful) 
 +  * ReflectionAttribute 
 +  * ReflectionClass 
 +  * ReflectionClassConstant 
 +  * ReflectionConstant 
 +  * ReflectionExtension 
 +  * ReflectionFiber 
 +  * ReflectionFunctionAbstract 
 +  * ReflectionGenerator 
 +  * ReflectionParameter 
 +  * ReflectionProperty 
 +  * ReflectionReference 
 +  * ReflectionType 
 +  * ReflectionZendExtension
  
-Some of those are containers of a sort, where this encoding is especially misleading (aside of <php>Generator</php> which was the motivator for this RFC, <php>WeakMap</php> stands out specifically) 
  
 === Backed by temporary resources === === Backed by temporary resources ===
   * AddressInfo   * AddressInfo
-  * CURLFile 
   * Collator   * Collator
   * CurlHandle   * CurlHandle
   * CurlMultiHandle   * CurlMultiHandle
   * CurlShareHandle   * CurlShareHandle
-  * DOMXPath 
   * DOMXPath   * DOMXPath
   * Dba\Connection   * Dba\Connection
Line 64: Line 90:
   * Dom\TokenList   * Dom\TokenList
   * Dom\XPath   * Dom\XPath
 +  * Dom\XMLDocument
   * EnchantBroker   * EnchantBroker
   * EnchantDictionary   * EnchantDictionary
Line 93: Line 120:
   * OpenSSLCertificateSigningRequest   * OpenSSLCertificateSigningRequest
   * PDO   * PDO
-  * PDORow 
-  * PDOStatement 
   * Pdo\Dblib   * Pdo\Dblib
   * Pdo\Firebird   * Pdo\Firebird
Line 110: Line 135:
   * SQLite3Stmt   * SQLite3Stmt
   * Shmop   * Shmop
-  * SimpleXMLElement 
   * Soap\Sdl   * Soap\Sdl
   * Soap\Url   * Soap\Url
Line 130: Line 154:
   * Generator   * Generator
   * InternalIterator   * InternalIterator
-  * ReflectionAttribute 
-  * ReflectionClass 
-  * ReflectionClassConstant 
-  * ReflectionConstant 
-  * ReflectionExtension 
-  * ReflectionFiber 
-  * ReflectionFunctionAbstract 
-  * ReflectionGenerator 
-  * ReflectionParameter 
-  * ReflectionProperty 
-  * ReflectionReference 
-  * ReflectionType 
-  * ReflectionZendExtension 
   * SensitiveParameterValue   * SensitiveParameterValue
   * WeakMap   * WeakMap
   * WeakReference   * WeakReference
 +
  
 ===== Backward Incompatible Changes ===== ===== Backward Incompatible Changes =====
rfc/deprecate-json_encode-nonserializable.txt · Last modified: 2024/09/05 10:00 by pilif