« Flextrine chosen for elevator pitch at Flash on the Beach 2010!
» Flextrine 0.5.1 released

AMFPHP, Actionscript 3.0, Flex 3, Flex 4, Object Oriented Design, PHP

Getting AMFPHP to turn NaN into null

06.17.10 | Comment?

Annoyingly the NaN (not a number) value of Number objects in AS3 is often quite hard to translate into a server-side equivalent and ends up breaking AMFPHP, and apparently BlazeDS too.

Here is a simple modification that will make AMFPHP turn NaN into a PHP null value on de-serialization.  Unfortunately on the return journey – trying to return NaN to AS3 – there doesn’t seem to be anything clever that can be done and the Flash Player insists on turning undefined numbers into 0.  A glance through the AMF spec doesn’t reveal anything useful, and if you need this functionality you are best off creating a custom class wrapping the value of the number and including a isNotANumber:Boolean or something that you can examine and set on the server.

Anyway, here’s the code.  In AMFBaseDeserializer.php find the following function:

   1: function readDouble() {

   2:     $bytes = substr($this->raw_data, $this->current_byte, 8);

   3:     $this->current_byte += 8;

   4:     if ($this->isBigEndian) {

   5:          $bytes = strrev($bytes);

   6:     } 

   7:     $zz = unpack("dflt", $bytes); // unpack the bytes

   8:     

   9:     return $zz['flt']; // return the number from the associative array

  10: } 

and change the last line so it reads:

   1: function readDouble() {

   2:     $bytes = substr($this->raw_data, $this->current_byte, 8);

   3:     $this->current_byte += 8;

   4:     if ($this->isBigEndian) {

   5:          $bytes = strrev($bytes);

   6:     } 

   7:     $zz = unpack("dflt", $bytes); // unpack the bytes

   8:     

   9:     return ($zz['flt'] != ‘NAN’) ? $zz['flt'] : null; // return the number from the associative array

  10: } 

Hope that helps someone!

Social bookmarks:
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google

have your say

Add your comment below, or trackback from your own site. Subscribe to these comments.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

:

:


« Flextrine chosen for elevator pitch at Flash on the Beach 2010!
» Flextrine 0.5.1 released