First, you'll just end up confusing yourself again if you call a variable used to hold a datastructure "$json". Also, printing to a file is just adding unnecessary complexity that makes it harder to test. Just print, and see the output. Here's a small script with those adaptations:

#!/usr/bin/env perl use strict; use warnings; use JSON::PP; my $data = { 'book' => { 'title' => JSON::PP::true, 'pages' => 5.03 #this number } }; my $json = encode_json($data); print $json, "\n"; __END__ {"book":{"pages":5.03,"title":true}}

The part after __END__ is the output I saw on my screen. It indicates that you are indeed getting 5.03, the number. Are you seeing different results? If so, it probably has to do with the heuristics that JSON::PP uses to decide if something is a number or a string. It might do some introspection to determine if the number has ever been used as a string. Examples of what can make Perl think internally that a scalar variable holds a string include obvious string operations such as concatenation, less obvious such as sprintf, and even the common case of using print.

Your actual code may be something like pages => $number, but you may at one point have treated $number as a string. And in so doing, Perl starts thinking the variable holds a string (this is an internal process... an implementation detail leaking out). You can force numification by using a numerical operator at a carefully chosen time:

my $data = { book => { title => JSON::PP::true, pages => 0+$number, } };

Now we're forcing numeric context on $number.


Dave


In reply to Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled by davido
in thread Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled by CropCircle

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.