| 1 |
# copied over from JSON::XS and modified to use JSON |
| 2 |
|
| 3 |
use strict; |
| 4 |
use warnings; |
| 5 |
use Test::More; |
| 6 |
BEGIN { plan tests => 78 + 2 }; |
| 7 |
|
| 8 |
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } |
| 9 |
|
| 10 |
use utf8; |
| 11 |
use JSON; |
| 12 |
|
| 13 |
|
| 14 |
ok (!defined JSON->new->allow_nonref (1)->decode ('null')); |
| 15 |
ok (JSON->new->allow_nonref (1)->decode ('true') == 1); |
| 16 |
ok (JSON->new->allow_nonref (1)->decode ('false') == 0); |
| 17 |
|
| 18 |
my $true = JSON->new->allow_nonref (1)->decode ('true'); |
| 19 |
ok ($true eq 1); |
| 20 |
ok (JSON::is_bool $true); |
| 21 |
my $false = JSON->new->allow_nonref (1)->decode ('false'); |
| 22 |
ok ($false == !$true); |
| 23 |
ok (JSON::is_bool $false); |
| 24 |
ok (++$false == 1); |
| 25 |
ok (!JSON::is_bool $false); |
| 26 |
ok (!JSON::is_bool "JSON::Boolean"); |
| 27 |
ok (!JSON::is_bool {}); # GH-34 |
| 28 |
|
| 29 |
ok (JSON->new->allow_nonref (1)->decode ('5') == 5); |
| 30 |
ok (JSON->new->allow_nonref (1)->decode ('-5') == -5); |
| 31 |
ok (JSON->new->allow_nonref (1)->decode ('5e1') == 50); |
| 32 |
ok (JSON->new->allow_nonref (1)->decode ('-333e+0') == -333); |
| 33 |
ok (JSON->new->allow_nonref (1)->decode ('2.5') == 2.5); |
| 34 |
|
| 35 |
ok (JSON->new->allow_nonref (1)->decode ('""') eq ""); |
| 36 |
ok ('[1,2,3,4]' eq encode_json decode_json ('[1,2, 3,4]')); |
| 37 |
ok ('[{},[],[],{}]' eq encode_json decode_json ('[{},[], [ ] ,{ }]')); |
| 38 |
ok ('[{"1":[5]}]' eq encode_json [{1 => [5]}]); |
| 39 |
ok ('{"1":2,"3":4}' eq JSON->new->canonical (1)->encode (decode_json '{ "1" : 2, "3" : 4 }')); |
| 40 |
ok ('{"1":2,"3":1.2}' eq JSON->new->canonical (1)->encode (decode_json '{ "1" : 2, "3" : 1.2 }')); |
| 41 |
|
| 42 |
ok ('[true]' eq encode_json [JSON::true]); |
| 43 |
ok ('[false]' eq encode_json [JSON::false]); |
| 44 |
ok ('[true]' eq encode_json [\1]); |
| 45 |
ok ('[false]' eq encode_json [\0]); |
| 46 |
ok ('[null]' eq encode_json [undef]); |
| 47 |
ok ('[true]' eq encode_json [JSON::true]); |
| 48 |
ok ('[false]' eq encode_json [JSON::false]); |
| 49 |
|
| 50 |
SKIP: { |
| 51 |
skip "core booleans not supported", 2 |
| 52 |
unless JSON->backend->can("CORE_BOOL") && JSON->backend->CORE_BOOL; |
| 53 |
|
| 54 |
ok ('[true]' eq encode_json [!!1]); |
| 55 |
ok ('[false]' eq encode_json [!!0]); |
| 56 |
} |
| 57 |
|
| 58 |
for my $v (1, 2, 3, 5, -1, -2, -3, -4, 100, 1000, 10000, -999, -88, -7, 7, 88, 999, -1e5, 1e6, 1e7, 1e8) { |
| 59 |
ok ($v == ((decode_json "[$v]")->[0])); |
| 60 |
ok ($v == ((decode_json encode_json [$v])->[0])); |
| 61 |
} |
| 62 |
|
| 63 |
ok (30123 == ((decode_json encode_json [30123])->[0])); |
| 64 |
ok (32123 == ((decode_json encode_json [32123])->[0])); |
| 65 |
ok (32456 == ((decode_json encode_json [32456])->[0])); |
| 66 |
ok (32789 == ((decode_json encode_json [32789])->[0])); |
| 67 |
ok (32767 == ((decode_json encode_json [32767])->[0])); |
| 68 |
ok (32768 == ((decode_json encode_json [32768])->[0])); |
| 69 |
|
| 70 |
my @sparse; @sparse[0,3] = (1, 4); |
| 71 |
ok ("[1,null,null,4]" eq encode_json \@sparse); |
| 72 |
|