Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87157 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 63760 invoked from network); 13 Jul 2015 19:27:47 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 13 Jul 2015 19:27:47 -0000 Authentication-Results: pb1.pair.com smtp.mail=php@golemon.com; spf=softfail; sender-id=softfail Authentication-Results: pb1.pair.com header.from=php@golemon.com; sender-id=softfail Received-SPF: softfail (pb1.pair.com: domain golemon.com does not designate 209.85.217.179 as permitted sender) X-PHP-List-Original-Sender: php@golemon.com X-Host-Fingerprint: 209.85.217.179 mail-lb0-f179.google.com Received: from [209.85.217.179] ([209.85.217.179:36012] helo=mail-lb0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 08/8E-43998-03114A55 for ; Mon, 13 Jul 2015 15:27:45 -0400 Received: by lbbpo10 with SMTP id po10so130846683lbb.3 for ; Mon, 13 Jul 2015 12:27:41 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:sender:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=SPZAn6DGG9tSCiOnRtGmexMN4a4msi3jtkLUgtg/C0Y=; b=AW4OmvR35p14aQvJ8qerZstzg8iKwZnBKnYA9Mgfk0kjQ8yY0lPMooXTpCPH1yLmKN Ula4xI9SlqshhhdFrpwncVtQ2mkZPzCEc2D+9jDek2tK4k9gScEROQOTh5hYA+zOBvwT 2pq5CVFXU0Wh8FfpzjxXibPC3ennJ/9iG53kc6Wv83oBYMnG3piPU+gj9zfzUQVOmpLP 9ZRIQLOuXqWnq4Yk5lFkzJJSWQ1y5OZIGVdhCAJrRjXmy7bFlBw6PL39orqHSKrDg+av wqjnKArXuPoKAC3vB9w5pTPfbM9ChXYlyGU9NgtpzlD5iCJtoiFi3eaQbJXIAU41rv3O UOvQ== X-Gm-Message-State: ALoCoQk/6LuZ4eaOyyDvE82PG+BLSTCCDrtYgHstaJwC6n785zuS7D3xVHO1nhYuiYdM3kwJEc/K MIME-Version: 1.0 X-Received: by 10.112.185.100 with SMTP id fb4mr34087343lbc.79.1436815661859; Mon, 13 Jul 2015 12:27:41 -0700 (PDT) Sender: php@golemon.com Received: by 10.112.11.134 with HTTP; Mon, 13 Jul 2015 12:27:41 -0700 (PDT) X-Originating-IP: [2620:10d:c081:f80::8:f2c0] In-Reply-To: References: Date: Mon, 13 Jul 2015 12:27:41 -0700 X-Google-Sender-Auth: GOvpxEPiGVbcSrWDG3QUJIqylYo Message-ID: To: Ryan Pallas Cc: Dean Eigenmann , "guilhermeblanco@gmail.com" , sbj.ml.read@gmail.com, "internals@lists.php.net" Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] JsonSerializable New Interface method Proposal From: pollita@php.net (Sara Golemon) On Mon, Jul 13, 2015 at 8:03 AM, Ryan Pallas 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