rfc:jsonserializable

This is an old revision of the document!


PHP RFC: JsonSerializable New Interface method Proposa

Introduction

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.

Proposal

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)
{
  $this->setName($json['name']);
}

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.

Proposed PHP Version(s)

next PHP 7.x

References

Rejected Features

rfc/jsonserializable.1436793622.txt.gz · Last modified: 2017/09/22 13:28 (external edit)