json(Raw)Serializable?

From: Date: Tue, 24 Oct 2017 00:20:30 +0000
Subject: json(Raw)Serializable?
Groups: php.internals 
Request: Send a blank email to internals+get-100920@lists.php.net to get a copy of this message
https://bugs.php.net/bug.php?id=75400 is asking for a version of JsonSerializable which doesn't involve deserializing/reserialzing round trips when a chunk of JSON is known in advance. It's not terribly unreasonable IMO, but before I just writeup the RFC as described (jsonRawSerialize taking preceedence over jsonSerialize), I thought I'd ask for opinions on the specifics. In psuedo-code: if (is_object($obj)) { if ($obj implements JsonRawSerializable) { // use $obj->jsonRawSerialize() as is. } elseif ($obj implements JsonSerializble) { // use json_encode($obj->jsonSerialize()) } else { // Serialize the object's public properties as a key/value map } } Perhaps with the stipulation that if jsonRawSerialize() returns null, we'd fallback on jsonSerialize(). Any other non-string results in an encoding error. -Sara

« previous php.internals (#100920) next »