CropCircle has asked for the wisdom of the Perl Monks concerning the following question:

Hi! I try to write json structure in file, one of the fields contains a number. So I get an error, which points out that this number is an encountered object:

use strict; use warnings; use JSON::PP; my $json = { 'book' => { 'title' => JSON::PP::true, 'pages' => 5.03 #this number } }; open my $fh, '>', 'data_out.json'; print $fh encode_json($json); #this line is in error close $fh;

If I make 'pages' => '5.03' - there are no erros BUT I need exactly 5.03 without quotes. Is there any way to make it work? THanks!

UPDATED!

use strict; use warnings; #convert all constant numbers to objects #it converted 5.03 to an object and it caused the error use bignum; use JSON::PP; # some code where "bignum" is used # ... no bignum; # exclude bignum usage my $json = { 'book' => { 'title' => JSON::PP::true, 'pages' => 5.03 #this number } }; open my $fh, '>', 'data_out.json'; print $fh encode_json($json); #this line is in error close $fh;

Also I checked this code for perl 5.10 and 5.20 - I get the same errors when using "bignum" and have no errors in both cases without "bignum". So, there are NO version dependencies (in this case)

  • Comment on Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
  • Select or Download Code

Replies are listed 'Best First'.
Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
by davido (Cardinal) on Aug 13, 2015 at 16:55 UTC

    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

Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
by tangent (Parson) on Aug 13, 2015 at 16:59 UTC
    I am not getting that error either, but are you sure that is your exact script - e.g. if you print the number before encoding it becomes a string. You can numify it by adding 0:
    my $json = { 'book' => { 'title' => JSON::PP::true, 'pages' => 5.03 } }; print $json->{'book'}{'pages'}, "\n"; my $string = encode_json($json); print "After printing: $string\n"; $json->{'book'}{'pages'} += 0; my $string2 = encode_json($json); print "After numify: $string2\n";
    Output:
    5.03 After printing: {"book":{"title":true,"pages":"5.03"}} After numify: {"book":{"title":true,"pages":5.03}}
Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
by Your Mother (Archbishop) on Aug 13, 2015 at 16:50 UTC

    Upgrade? I get this (with JSON::PP 2.27203)–

    {"book":{"title":true,"pages":5.03}}
Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
by ikegami (Patriarch) on Aug 13, 2015 at 20:59 UTC
    Please post code that actually generates the problem. Maybe the code you used included use bignum;?
      Yes, I used bignum and it caused this error. Before this code block I added "no bignum" (couldn`t remove it, because it used before this block of code)
        use bignum; changes 5.03 to Math::BigFloat->new('5.03').
      It`s rather strange, however, I run this script for perl 5.10 - and it worked there,but for perl 5.20 - no, this error occures.
Re: Encountered object '5.03 ', but neither allow_blessed nor convert_blessed settings are enabled
by Bod (Parson) on Jun 20, 2024 at 18:20 UTC

    I'm getting a very similar error...

    my $payload = { 'sender' => { 'name' => $vars{'fromname'}, 'email' => $vars{'frommail'}, }, 'to' => [{ 'name' => $vars{'name'}, 'email' => $vars{'email'}, }], 'subject' => $vars{'subject'}, 'HTMLcontent' => $body, }; $payload->{'textContent'} = $vars{'preview'} if $vars{'preview'}; my $attrs = { 'headers' => $headers, 'content' => encode_json $payload, };

    This is part of the email module we use to send emails through Brevo. But I'm seeing an error...

    encountered object 'Site::Email=HASH(0x55fa0a139c18)', but neither all +ow_blessed, convert_blessed nor allow_tags settings are enabled (or T +O_JSON/FREEZE method missing)

    The error line is encode_json.

    The weird part is that nearly identical code is running elsewhere on the same server without a problem! It's been suggested that bignum could affect it but we are not using that module in either place.

    I'm aware of this bug report but that wouldn't explain why one place fails and another doesn't.

    Dumpling $payload gives the result one would expect - any ideas on how I further debug this issue?

    This is Perl v5.36 on Debian 12

    UPDATE...

    The problem turned out to be the way I was calling the module...and using the same variable twice!

    my $email = Site::Email->new; my $resp = $email->send( 'fname' => $fname, 'user' => "$prefix$data{'idCRM'}", 'token' => $token, 'template' => 'newuser', 'name' => $to, 'email' => $email, 'subject' => 'Your Book Boost Account...' );