PHP RFC: JsonSerializable New Interface method Proposal


JSON in php currently supports the Serialization of objects using the \JsonSerializable interface. However it does not support the unserialization of JSON strings to objects.


The concept of this interface is to offer the functionality that is displayed in the \Serializable interface. The implementation could be used in a new Interface or extended in the JsonSerializable Interface.

With extension to the JsonSerializable interface there could be new functionality brought to it that would support unserialization. A code snippet would then look like this:

$user = (User) json_decode('{"name": "Foo"}');

Within the class User there would then be function that receives the decoded JSON.

public function jsonUnserialize($json)

There are multiple use cases for this object, like an easier way of communicating with REST APIs, as the return can simply be written into a predefined object.

Unlike the ArrayObject, an object like this would not be accessable like an array, and would have an easier support of private variables as the setters are in the class itself. The interface would just ensure that a json_decode would trigger the jsonUnserialize method with the json.

Updated Proposal

Due to discussions, it seems the best way to implement this method is with a new function.

$user = json_decode_to_class($json, User::class);


Real name Yes No
Count: 0 0

Proposed PHP Version(s)

next PHP 7.x


Rejected Features

rfc/jsonserializable.txt · Last modified: 2017/09/22 13:28 by