Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Sebastian B.-Hagensen
Hi,

I like the general idea behind that proposal.

I'm not sure how you would want to implement that however (please
expand the rfc on that topic).
Do you want to give the cast syntax a special meaning if used in
connection with json_decode or add the general ability to
cast a stdClass object (as returned by json_decode) to a specific user
type or do you want to change json_decode to return a new class
(extending stdclass) that can be casted in the way you want? The first
two could be considered fundamental changes to the language, the third
may include serious bc concerns.

Wouldn't an additional function (maybe in addition to your proposed
interface; example: json_decode_to(string $json, string $className,
int $options)) solve the issue without changing the language?

Regards,


2015-07-13 15:22 GMT+02:00 Dean Eigenmann dean.eigenm...@icloud.com:
 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable
 regarding Json to Object unserialization.

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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Ryan Pallas


 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.


[PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann

Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable regarding 
Json to Object unserialization.

Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann

The Additional function you have proposed seems like the easiest and best way 
to do it currently without changing the language. I was thinking of giving the 
cast syntax special meaning if used in connection with json_decode, but this 
would most likely be near to impossible.

On Jul 13, 2015, at 04:08 PM, Sebastian B.-Hagensen sbj.ml.r...@gmail.com 
wrote:

Hi,

I like the general idea behind that proposal.

I'm not sure how you would want to implement that however (please
expand the rfc on that topic).
Do you want to give the cast syntax a special meaning if used in
connection with json_decode or add the general ability to
cast a stdClass object (as returned by json_decode) to a specific user
type or do you want to change json_decode to return a new class
(extending stdclass) that can be casted in the way you want? The first
two could be considered fundamental changes to the language, the third
may include serious bc concerns.

Wouldn't an additional function (maybe in addition to your proposed
interface; example: json_decode_to(string $json, string $className,
int $options)) solve the issue without changing the language?

Regards,


2015-07-13 15:22 GMT+02:00 Dean Eigenmann dean.eigenm...@icloud.com:
Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable
regarding Json to Object unserialization.


Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread guilhermebla...@gmail.com
What about JsonDeserializable? I would like to have the choice to have a
serialize-only operation.

On Mon, Jul 13, 2015 at 10:13 AM, Dean Eigenmann dean.eigenm...@icloud.com
wrote:

 The Additional function you have proposed seems like the easiest and best
 way to do it currently without changing the language. I was thinking of
 giving the cast syntax special meaning if used in connection with
 json_decode, but this would most likely be near to impossible.

 On Jul 13, 2015, at 04:08 PM, Sebastian B.-Hagensen 
 sbj.ml.r...@gmail.com wrote:

 Hi,

 I like the general idea behind that proposal.

 I'm not sure how you would want to implement that however (please
 expand the rfc on that topic).
 Do you want to give the cast syntax a special meaning if used in
 connection with json_decode or add the general ability to
 cast a stdClass object (as returned by json_decode) to a specific user
 type or do you want to change json_decode to return a new class
 (extending stdclass) that can be casted in the way you want? The first
 two could be considered fundamental changes to the language, the third
 may include serious bc concerns.

 Wouldn't an additional function (maybe in addition to your proposed
 interface; example: json_decode_to(string $json, string $className,
 int $options)) solve the issue without changing the language?

 Regards,


 2015-07-13 15:22 GMT+02:00 Dean Eigenmann dean.eigenm...@icloud.com:

 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable

 regarding Json to Object unserialization.




-- 
Guilherme Blanco
MSN: guilhermebla...@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada


Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Rowan Collins

guilhermebla...@gmail.com wrote on 13/07/2015 15:19:

What about JsonDeserializable? I would like to have the choice to have a
serialize-only operation.


Yeah, this would need to be a new interface, because interfaces can't 
have optional methods, so changing the existing interface would 
instantly break all existing implementations, which would be a big no-no 
IMHO.


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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann

I think the suggested decode function would be the best way to handle this.

On Jul 13, 2015, at 04:20 PM, guilhermebla...@gmail.com 
guilhermebla...@gmail.com wrote:

What about JsonDeserializable? I would like to have the choice to have a
serialize-only operation.

On Mon, Jul 13, 2015 at 10:13 AM, Dean Eigenmann dean.eigenm...@icloud.com
wrote:

The Additional function you have proposed seems like the easiest and best
way to do it currently without changing the language. I was thinking of
giving the cast syntax special meaning if used in connection with
json_decode, but this would most likely be near to impossible.

On Jul 13, 2015, at 04:08 PM, Sebastian B.-Hagensen 
sbj.ml.r...@gmail.com wrote:

Hi,

I like the general idea behind that proposal.

I'm not sure how you would want to implement that however (please
expand the rfc on that topic).
Do you want to give the cast syntax a special meaning if used in
connection with json_decode or add the general ability to
cast a stdClass object (as returned by json_decode) to a specific user
type or do you want to change json_decode to return a new class
(extending stdclass) that can be casted in the way you want? The first
two could be considered fundamental changes to the language, the third
may include serious bc concerns.

Wouldn't an additional function (maybe in addition to your proposed
interface; example: json_decode_to(string $json, string $className,
int $options)) solve the issue without changing the language?

Regards,


2015-07-13 15:22 GMT+02:00 Dean Eigenmann dean.eigenm...@icloud.com:

Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable

regarding Json to Object unserialization.




--
Guilherme Blanco
MSN: guilhermebla...@hotmail.com
GTalk: guilhermeblanco
Toronto - ON/Canada


Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Aaron Piotrowski

 On Jul 13, 2015, at 1:56 PM, Dean Eigenmann dean.eigenm...@icloud.com wrote:
 
 I have updated the RFC with a new section showing the updates
 
 Sent from my iPhone
 
 On 13 Jul 2015, at 20:47, Benjamin Eberlei kont...@beberlei.de wrote:
 
 On Mon, Jul 13, 2015 at 3:22 PM, Dean Eigenmann dean.eigenm...@icloud.com
 wrote:
 
 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable 
 regarding
 Json to Object unserialization.
 
 The approach with typecasting will not work, because the function
 json_decode doesn't have that information.
 
 Instead something like the JMS Serializer api is probably necessary: $user
 = json_decode($data, 'User');
 
 I agree with Guilherme that we need a second interface for this, especially
 because changing the existing one would be a BC break.
 
 --
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php
 

What are the advantages of the proposed functionality versus writing a static 
constructor?

Writing $user = json_decode_to_class($json, User::class); doesn’t seem to offer 
anything over $user = User::fromJson($json);. In my opinion, it seems less 
obvious what is happening in the former case and the latter case requires no 
additional functions or modification to the engine.

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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Sara Golemon
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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Jakub Zelenka
Hi

On 13 Jul 2015 14:23, Dean Eigenmann dean.eigenm...@icloud.com wrote:

 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable
regarding Json to Object unserialization.

As it's been said, the first part (casting) is a no-go. It would have to be
done in a run time by scanning of opcode array which is not nice.

The updated proposal ( json_decode_to_class ) could be implemented with
small modification (missing options param for decoding options) but the
general idea is limited to top level object. It means that you cannot use
it on array of objects or nested objects (properties objects).

This is however something that could be addressed by extended JsonSchema
that I described

https://www.mail-archive.com/internals@lists.php.net/msg78698.html

As I said before, such implementation will take much more time as I have
got couple of more other things on my list that I want to do on json before
that (sort of prerequisite). It means that the question is if we want an
uncomplete limited solution now or wait slightly longer for a proper
solution. My opinion is that we should wait longer and have a proper
solution.

Cheers

Jakub


Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann
I have updated the RFC with a new section showing the updates

Sent from my iPhone

 On 13 Jul 2015, at 20:47, Benjamin Eberlei kont...@beberlei.de wrote:
 
 On Mon, Jul 13, 2015 at 3:22 PM, Dean Eigenmann dean.eigenm...@icloud.com
 wrote:
 
 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable regarding
 Json to Object unserialization.
 
 The approach with typecasting will not work, because the function
 json_decode doesn't have that information.
 
 Instead something like the JMS Serializer api is probably necessary: $user
 = json_decode($data, 'User');
 
 I agree with Guilherme that we need a second interface for this, especially
 because changing the existing one would be a BC break.

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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Benjamin Eberlei
On Mon, Jul 13, 2015 at 3:22 PM, Dean Eigenmann dean.eigenm...@icloud.com
wrote:

 Ive just opened a new RFC https://wiki.php.net/rfc/jsonserializable regarding
 Json to Object unserialization.


The approach with typecasting will not work, because the function
json_decode doesn't have that information.

Instead something like the JMS Serializer api is probably necessary: $user
= json_decode($data, 'User');

I agree with Guilherme that we need a second interface for this, especially
because changing the existing one would be a BC break.


Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann
I will update the spec for it to be a function, called something like 
json_decode_to($json, $class, $mapping = []);

With mapping you could then specify with array keys map to which class property.

Sent from my iPhone

 On 13 Jul 2015, at 16:59, Rowan Collins rowan.coll...@gmail.com wrote:
 
 guilhermebla...@gmail.com wrote on 13/07/2015 15:19:
 What about JsonDeserializable? I would like to have the choice to have a
 serialize-only operation.
 
 Yeah, this would need to be a new interface, because interfaces can't have 
 optional methods, so changing the existing interface would instantly break 
 all existing implementations, which would be a big no-no IMHO.
 
 -- 
 PHP Internals - PHP Runtime Development Mailing List
 To unsubscribe, visit: http://www.php.net/unsub.php
 

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



Re: [PHP-DEV] JsonSerializable New Interface method Proposal

2015-07-13 Thread Dean Eigenmann
Ryan,

I think the usage of this won't support multidimensional arrays.

However in the decode function, 
You could specify

Foreach ($arr['permission'] as $permission) {
$this-permissions[] = (permission) json_decode($permission);
}

Sent from my iPhone

On 13 Jul 2015, at 17:03, 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.