I am trying to encode a perl nested hash and send it to some web application. Somehow the json encoder converts the numbers or floats to strings. The web application sees the data as strings and can't plot the chart. I can add code in the web application to convert them back to numbers, but I am looking for better solution of not having the numbers as strings in the first place.
Here is the code:
use strict;
use warnings;
use CGI qw/param/;
use JSON::XS;
my $json_obj = JSON::XS->new->allow_nonref;
## Build some Perl data
my %perl_data;
$perl_data{'numbers'}{'nested'} = [qw/1 -2 4 2 5 6/] ;
$perl_data{'mix'}{'AnotherLevel'} = [qw/null "Temp" 4 2 5 6/] ;
print "Content-type: text/html\n\n";
print $json_obj->pretty->encode(\%perl_data);
Here is the output where everything is just stringified:
Content-type: text/html
{
"numbers" : {
"nested" : [
"1",
"-2",
"4",
"2",
"5",
"6"
]
},
"mix" : {
"AnotherLevel" : [
"null",
"\"Temp\"",
"4",
"2",
"5",
"6"
]
}
}
In the above code, I even tried the following, but to no avail.
use JSON;
my $json_obj = JSON;
Any help is greatly appreciated.