# RFC: Extending the JsonSerializable interface

- Status: Withdrawn
- Authors: Dean Eigenmann, dean.eigenmann@icloud.com
- Date: 2015-07-13
- Version: 0.9
- Original: https://wiki.php.net/rfc/jsonserializable
- Page: https://externals.io/rfc/jsonserializable
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

## Votes

### jsonserializable (closed)

Yes: 0 · No: 0

## Discussions on the mailing list

- [JsonSerializable New Interface method Proposal](https://externals.io/message/87142.md) — started on 2015-07-13, 6 messages
- [JsonSerializable New Interface method Proposal](https://externals.io/message/87144.md) — started on 2015-07-13, 4 messages
- [JsonSerializable New Interface method Proposal](https://externals.io/message/87146.md) — started on 2015-07-13, 4 messages

---

## 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:

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

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

```php
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.

## Updated Proposal

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

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

## Vote

**Vote: jsonserializable** — Yes: 0 · No: 0 (closed)

## Proposed PHP Version(s)

next PHP 7.x

## References

[ Blog Post](https://decanus.github.io/JsonSerializable-Proposal/)

## Rejected Features
