Is it actually a problem that JSON is making 123.1 into a '123.1' as it goes into mongodb? If you are pulling it back out again in perl, then it shouldn't matter.

If it does matter, then the workaround is going to be dependant on what library does the serialization. If it is JSON.pm, then what you are doing ( add zero, or multiply by 1 ) should make sure that things are encoded as numbers.

It appears that JSON.pm is doing things correctly.

use JSON::PP; use JSON::XS; use JSON::Syck; my $x = 123.1; my $y = '123.10'; my $z = $y + 0; my $h = { x => $x, y => $y, z => $z }; print JSON::PP::encode_json($h) . "\n"; print JSON::XS::encode_json($h) . "\n"; print JSON::Syck::Dump($h) . "\n"; __END__ {"y":"123.10","x":123.1,"z":123.1} #JSON::PP {"y":"123.10","x":123.1,"z":123.1} #JSON::XS {"y":123.10,"x":123.1,"z":123.1} #JSON::Syck ( seems to do $y wrong/ +differently )
-- AlexLC

In reply to Re^4: Need some wisdom on strings to numbers by alexlc
in thread Need some wisdom on strings to numbers by decebel

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.