1 2use strict; 3use Test::More; 4BEGIN { plan tests => 9 }; 5 6BEGIN { $ENV{PERL_JSON_BACKEND} = 0; } 7 8use JSON::PP; 9 10eval q| require Math::BigInt |; 11 12SKIP: { 13 skip "Can't load Math::BigInt.", 9 if ($@); 14 15 my $v = Math::BigInt->VERSION; 16 $v =~ s/_.+$// if $v; 17 18my $fix = !$v ? '+' 19 : $v < 1.6 ? '+' 20 : ''; 21 22 23my $json = new JSON::PP; 24 25$json->allow_nonref->allow_bignum(1); 26$json->convert_blessed->allow_blessed; 27 28my $num = $json->decode(q|100000000000000000000000000000000000000|); 29 30ok($num->isa('Math::BigInt')); 31is("$num", $fix . '100000000000000000000000000000000000000'); 32is($json->encode($num), $fix . '100000000000000000000000000000000000000'); 33 34{ #SKIP_UNLESS_PP 2.91_03, 2 35$num = $json->decode(q|10|); 36 37ok(!(ref $num and $num->isa('Math::BigInt')), 'small integer is not a BigInt'); 38ok(!(ref $num and $num->isa('Math::BigFloat')), 'small integer is not a BigFloat'); 39} 40 41$num = $json->decode(q|2.0000000000000000001|); 42 43ok($num->isa('Math::BigFloat')); 44is("$num", '2.0000000000000000001'); 45is($json->encode($num), '2.0000000000000000001'); 46 47{ #SKIP_UNLESS_PP 2.90, 1 48is($json->encode([Math::BigInt->new("0")]), "[${fix}0]", "zero bigint is 0 (the number), not '0' (the string)" ); 49} 50} 51