On Mon, Jul 13, 2015 at 8:03 AM, Ryan Pallas <derokor...@gmail.com> wrote:
>> Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable
>> regarding Json to Object unserialization.
>>
>> I like the idea, but how do you handle complex json notations, that may
> contain arrays of objects? Say:
> {
>    "id": 123
>    "type": "user",
>    "name": "derokorian"
>    "permissions": [{
>       "id": 1,
>       "value": "create"
>     },{
>       "id": 2,
>      "value": "edit"
>     }]
> }
>
> Would this new function give me objects of type Permission in the
> permissions array, or would this solution only be a single level deep and
> therefore return an array of StdClass? I see no way to be able to tell this
> to the decoder in the User class because php does not support typed arrays.
>
This is my concern as well.  There's also the fact that even for
simple objects, having the json_*() api aware of deserialization rules
is over-engineered.

$obj = json_decode_to_class($json, 'User');

Could as easily be written as:

$obj = User::createFromStdClass(json_decode($json));

And that's logic you can implement entirely in userspace without the
need to worry about what versions support that interface.  Firther,
for more complex ovhects like the one Ryan described above, your
createFrom*() functions have the context of how to deal with child
elements, e.g.:

class User {
  public static function createFromStdClass(stdClass $obj) {
    /* make an initial object from basic props... */
    foreach ($obj->permissions as $perm) {
      $ret->addPermission(UserPermissions::createFromStdClass($perm));
    }
    return $ret;
  }
}

I don't think we need a formal API in the standard runtime for this,
particularly as it fails at anything beyond the most simple use cases.
-Sara

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to