1use Test::More; 2 3# copied over from JSON::PC and modified to use JSON 4# copied over from JSON::XS and modified to use JSON 5 6use strict; 7BEGIN { plan tests => 20 }; 8 9BEGIN { $ENV{PERL_JSON_BACKEND} = "JSON::backportPP"; } 10 11use JSON; 12 13my ($js,$obj); 14 15my $pc = new JSON; 16 17$js = q|{}|; 18 19$obj = $pc->decode($js); 20$js = $pc->encode($obj); 21is($js,'{}', '{}'); 22 23$js = q|[]|; 24$obj = $pc->decode($js); 25$js = $pc->encode($obj); 26is($js,'[]', '[]'); 27 28 29$js = q|{"foo":"bar"}|; 30$obj = $pc->decode($js); 31is($obj->{foo},'bar'); 32$js = $pc->encode($obj); 33is($js,'{"foo":"bar"}', '{"foo":"bar"}'); 34 35$js = q|{"foo":""}|; 36$obj = $pc->decode($js); 37$js = $pc->encode($obj); 38is($js,'{"foo":""}', '{"foo":""}'); 39 40$js = q|{"foo":" "}|; 41$obj = $pc->decode($js); 42$js = $pc->encode($obj); 43is($js,'{"foo":" "}' ,'{"foo":" "}'); 44 45$js = q|{"foo":"0"}|; 46$obj = $pc->decode($js); 47$js = $pc->encode($obj); 48is($js,'{"foo":"0"}',q|{"foo":"0"} - autoencode (default)|); 49 50 51$js = q|{"foo":"0 0"}|; 52$obj = $pc->decode($js); 53$js = $pc->encode($obj); 54is($js,'{"foo":"0 0"}','{"foo":"0 0"}'); 55 56$js = q|[1,2,3]|; 57$obj = $pc->decode($js); 58is($obj->[1],2); 59$js = $pc->encode($obj); 60is($js,'[1,2,3]'); 61 62$js = q|{"foo":{"bar":"hoge"}}|; 63$obj = $pc->decode($js); 64is($obj->{foo}->{bar},'hoge'); 65$js = $pc->encode($obj); 66is($js,q|{"foo":{"bar":"hoge"}}|); 67 68$js = q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|; 69$obj = $pc->decode($js); 70$js = $pc->encode($obj); 71is($js,q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|); 72 73 74$obj = ["\x01"]; 75is($js = $pc->encode($obj),'["\\u0001"]'); 76$obj = $pc->decode($js); 77is($obj->[0],"\x01"); 78 79$obj = ["\e"]; 80is($js = $pc->encode($obj),'["\\u001b"]'); 81$obj = $pc->decode($js); 82is($obj->[0],"\e"); 83 84$js = '{"id":"}'; 85eval q{ $pc->decode($js) }; 86like($@, qr/unexpected end/i); 87 88$obj = { foo => sub { "bar" } }; 89eval q{ $js = $pc->encode($obj) }; 90like($@, qr/JSON can only/i, 'invalid value (coderef)'); 91 92#$obj = { foo => bless {}, "Hoge" }; 93#eval q{ $js = $pc->encode($obj) }; 94#like($@, qr/JSON can only/i, 'invalid value (blessd object)'); 95 96$obj = { foo => \$js }; 97eval q{ $js = $pc->encode($obj) }; 98like($@, qr/cannot encode reference/i, 'invalid value (ref)'); 99 100