[Proto-Scripty] Re: Detecting an empty [] as a parameter to a function.

2009-03-19 Thread Richard Quadling

2009/3/18 Richard Quadling rquadl...@googlemail.com:
 2009/2/25 SWilk wilkola...@gmail.com:

 kangax wrote:
 On Feb 25, 3:27 am, SWilk wilkola...@gmail.com wrote:
 [...]
 It's not that Richard does want to use it. It's that the PHP
 json_encode() function produces inconsistent output for empty arrays.

 If you do
 json_encode(array('key' = 'value');
 you will get:
 { key: value };

 And I assume that `json_encode(array('a', 'b', 'c'))` returns `[a,
 b, c]`?
 That's right.

 [...]

 I think in most cases:
 var _a=[];
 _a = (Object.isArray(_a)  !_a.length) ? {} : _a;
 var hash = $H(_a);

 That should work, but wouldn't you want to differentiate between these
 broken arrays (which should really be empty objects) and the actual
 empty arrays (returned from json)?

 That depends on the backend of course.
 For me, this would be rarely (if ever) a problem, cause I am trying
 to keep my returned types consistent.
 If I return an associative array from my backend, then it should be
 always associative array. And I would always expect plain Object in
 json response. I this case that workaround works.
 If I return numerically indexed array, then it should always be so,
 and then I always expect native Array in the response. In this case I
 would use $A instead of $H anyway, so no workaround is needed.

 Of course there might be persons who mix those types and return array
 of objects when found many, and just the object, when found one. I
 have no idea how to distinguish such cases... But this would require
 extra js logic anyway, so I think it would not be a problem.

 I agree with Richard, that there should be a warning for php users in
 the docs, and a sample snippet of js code providing a way to avoid
 accidental passing of empty Array to $H.

 I'll try to write some info covering this on proto-scripty.wikidot.com
 tonight. It might be of some help to many.

 If you have any better idea how to avoid this problem,
 let us know.

 Regards,
 SWilk

 


 PHP 5.3 has just had a mod done to force json_encode() to output an
 object hash. Currently it will output an array or an object hash
 depending upon the array content.

 The PHP manual will be updated later on today.

 http://docs.php.net/json_encode

 So, my issue is fixed. It would be useful to have something documented
 in the Prototype manual also. This fix in PHP will ONLY be for PHP
 V5.3.0 and above. RC1 of PHP5.3.0 is coming soon.

 Obviously, getting this out to the ISPs will take time (these things always 
 do).

 Regards,

 Richard Quadling.

 --
 -
 Richard Quadling
 Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
 Standing on the shoulders of some very clever giants!


PHP manual updated. Will be live by tomorrow.

-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Detecting an empty [] as a parameter to a function.

2009-03-18 Thread Richard Quadling

2009/2/25 SWilk wilkola...@gmail.com:

 kangax wrote:
 On Feb 25, 3:27 am, SWilk wilkola...@gmail.com wrote:
 [...]
 It's not that Richard does want to use it. It's that the PHP
 json_encode() function produces inconsistent output for empty arrays.

 If you do
 json_encode(array('key' = 'value');
 you will get:
 { key: value };

 And I assume that `json_encode(array('a', 'b', 'c'))` returns `[a,
 b, c]`?
 That's right.

 [...]

 I think in most cases:
 var _a=[];
 _a = (Object.isArray(_a)  !_a.length) ? {} : _a;
 var hash = $H(_a);

 That should work, but wouldn't you want to differentiate between these
 broken arrays (which should really be empty objects) and the actual
 empty arrays (returned from json)?

 That depends on the backend of course.
 For me, this would be rarely (if ever) a problem, cause I am trying
 to keep my returned types consistent.
 If I return an associative array from my backend, then it should be
 always associative array. And I would always expect plain Object in
 json response. I this case that workaround works.
 If I return numerically indexed array, then it should always be so,
 and then I always expect native Array in the response. In this case I
 would use $A instead of $H anyway, so no workaround is needed.

 Of course there might be persons who mix those types and return array
 of objects when found many, and just the object, when found one. I
 have no idea how to distinguish such cases... But this would require
 extra js logic anyway, so I think it would not be a problem.

 I agree with Richard, that there should be a warning for php users in
 the docs, and a sample snippet of js code providing a way to avoid
 accidental passing of empty Array to $H.

 I'll try to write some info covering this on proto-scripty.wikidot.com
 tonight. It might be of some help to many.

 If you have any better idea how to avoid this problem,
 let us know.

 Regards,
 SWilk

 


PHP 5.3 has just had a mod done to force json_encode() to output an
object hash. Currently it will output an array or an object hash
depending upon the array content.

The PHP manual will be updated later on today.

http://docs.php.net/json_encode

So, my issue is fixed. It would be useful to have something documented
in the Prototype manual also. This fix in PHP will ONLY be for PHP
V5.3.0 and above. RC1 of PHP5.3.0 is coming soon.

Obviously, getting this out to the ISPs will take time (these things always do).

Regards,

Richard Quadling.

-- 
-
Richard Quadling
Zend Certified Engineer : http://zend.com/zce.php?c=ZEND002498r=213474731
Standing on the shoulders of some very clever giants!

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---



[Proto-Scripty] Re: Detecting an empty [] as a parameter to a function.

2009-02-25 Thread kangax

On Feb 25, 3:27 am, SWilk wilkola...@gmail.com wrote:
[...]
 It's not that Richard does want to use it. It's that the PHP
 json_encode() function produces inconsistent output for empty arrays.

 If you do
 json_encode(array('key' = 'value');
 you will get:
 { key: value };

And I assume that `json_encode(array('a', 'b', 'c'))` returns `[a,
b, c]`?

[...]

 I think in most cases:
 var _a=[];
 _a = (Object.isArray(_a)  !_a.length) ? {} : _a;
 var hash = $H(_a);

That should work, but wouldn't you want to differentiate between these
broken arrays (which should really be empty objects) and the actual
empty arrays (returned from json)?

--
kangax
--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
Prototype  script.aculo.us group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en
-~--~~~~--~~--~--~---