1# copied over from JSON::XS and modified to use JSON 2 3use strict; 4use Test::More; 5BEGIN { plan tests => 16 }; 6 7BEGIN { $ENV{PERL_JSON_BACKEND} = "JSON::backportPP"; } 8 9use JSON; 10 11 12my $o1 = bless { a => 3 }, "XX"; 13my $o2 = bless \(my $dummy = 1), "YY"; 14 15sub XX::TO_JSON { 16 {'__',""} 17} 18 19my $js = JSON->new; 20 21eval { $js->encode ($o1) }; ok ($@ =~ /allow_blessed/); 22eval { $js->encode ($o2) }; ok ($@ =~ /allow_blessed/); 23$js->allow_blessed; 24ok ($js->encode ($o1) eq "null"); 25ok ($js->encode ($o2) eq "null"); 26$js->convert_blessed; 27ok ($js->encode ($o1) eq '{"__":""}'); 28 29ok ($js->encode ($o2) eq "null"); 30 31$js->filter_json_object (sub { 5 }); 32$js->filter_json_single_key_object (a => sub { shift }); 33$js->filter_json_single_key_object (b => sub { 7 }); 34 35ok ("ARRAY" eq ref $js->decode ("[]")); 36ok (5 eq join ":", @{ $js->decode ('[{}]') }); 37ok (6 eq join ":", @{ $js->decode ('[{"a":6}]') }); 38ok (5 eq join ":", @{ $js->decode ('[{"a":4,"b":7}]') }); 39 40$js->filter_json_object; 41ok (7 == $js->decode ('[{"a":4,"b":7}]')->[0]{b}); 42ok (3 eq join ":", @{ $js->decode ('[{"a":3}]') }); 43 44$js->filter_json_object (sub { }); 45ok (7 == $js->decode ('[{"a":4,"b":7}]')->[0]{b}); 46ok (9 eq join ":", @{ $js->decode ('[{"a":9}]') }); 47 48$js->filter_json_single_key_object ("a"); 49ok (4 == $js->decode ('[{"a":4}]')->[0]{a}); 50 51#$js->filter_json_single_key_object (a => sub {}); 52$js->filter_json_single_key_object (a => sub { return; }); # sub {} is not suitable for Perl 5.6 53ok (4 == $js->decode ('[{"a":4}]')->[0]{a}); 54