[Chicago-talk] Problem with JSON module

David J. Young younda at gmail.com
Sun Oct 4 14:06:44 PDT 2009


Thanks Andrew for the pointer to the Devel::Peek module.  I didn't know
about this module.
I had also found that Data::Dumper did very subtly show a difference between
a value internally stored in Perl as a string or as a number.  In the two
dump snippets shown below, the numeric value is unquoted, and the string
value is single quoted.  For my purposes, I added zero (+ 0) to my values
before putting them into the data structure that I was passing to the
"to_json" call.  This produced the JSON string I needed without the double
quotes.  Now my jQuery flot charts work perfectly.

Thanx for the help!


'data' => [                  'data' => [
            [                            [
              0,                           '0',
              '31.3794'                    '31.3794'
            ],                           ],
            [                            [
              1,                           '1',
              '57.6309'                    '57.6309'
            ],                           ],
            [                            [
              2,                           '2',
              0                            '0.0000'
            ],                           ],
            [                            [
              3,                           '3',
              '40.0247'                    '40.0247'
            ],                           ],



On Sat, Oct 3, 2009 at 1:50 AM, Andrew Rodland <andrew at cleverdomain.org>wrote:

> "x" in the debugger doesn't show it, but I would bet that JSON is keying on
> the magical, mostly-hidden type ("OK") flags of scalars. If you do $a =
> "12.34", $a has a valid string value but not a valid numeric value. If you
> calculate $a + 0, $a now has a valid string value *and* a valid numeric
> value,
> until it's changed by either a string operation or a numeric operation (at
> which point the value that wasn't part of the operation is marked as no
> longer
> "OK".) If you do $b = 12.34, $b has a valid numeric value but not a valid
> string value. If you then print $b, $b will have valid numeric *and* string
> parts, etc. etc.
>
> > $ perl -MDevel::Peek -le '$a = "12.34"; print "Before:"; Dump($a); 0 +
> $a;
> print "After:"; Dump($a)'
> > Before:
> > SV = PV(0xe58b68) at 0xe80ca8
> >   REFCNT = 1
> >   FLAGS = (POK,pPOK)
> >   PV = 0xe7ad00 "12.34"\0
> >   CUR = 5
> >   LEN = 8
> > After:
> > SV = PVNV(0xe5a0b0) at 0xe80ca8
> >   REFCNT = 1
> >   FLAGS = (NOK,POK,pIOK,pNOK,pPOK)
> >   IV = 12
> >   NV = 12.34
> >   PV = 0xe7ad00 "12.34"\0
> >   CUR = 5
> >   LEN = 8
>
> > $ perl -MDevel::Peek -le '$b = 12.34; print "Before:"; Dump($b); "" . $b;
> print "After:"; Dump($b)'
> > Before:
> > SV = NV(0x15810d0) at 0x1559ca8
> >   REFCNT = 1
> >   FLAGS = (NOK,pNOK)
> >   NV = 12.34
> > After:
> > SV = PVNV(0x15330b0) at 0x1559ca8
> >   REFCNT = 1
> >   FLAGS = (NOK,POK,pNOK,pPOK)
> >   IV = 0
> >   NV = 12.34
> >   PV = 0x157c020 "12.34"\0
> >   CUR = 5
> >   LEN = 40
>
> JSON is presumably turning JSON numeric literals into numeric scalars
> (storing
> 0 + $val), and likewise when it gets a value that has NOK or IOK but not
> POK,
> it outputs a numeric value without quotes.
>
> Andrew
> _______________________________________________
> Chicago-talk mailing list
> Chicago-talk at pm.org
> http://mail.pm.org/mailman/listinfo/chicago-talk
>



-- 
I take the "Shhhh" out of IT - ydy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/chicago-talk/attachments/20091004/7bf8cb78/attachment.html>


More information about the Chicago-talk mailing list