1 2use strict; 3use Test::More; 4BEGIN { plan tests => 3 }; 5 6BEGIN { $ENV{PERL_JSON_BACKEND} = 1; } 7 8use JSON -convert_blessed_universally; 9 10SKIP: { 11 skip "can't use JSON::XS.", 3, unless( JSON->backend->is_xs ); 12 13my $obj = Test->new( [ 1, 2, {foo => 'bar'} ] ); 14 15$obj->[3] = Test2->new( { a => 'b' } ); 16 17my $json = JSON->new->allow_blessed->convert_blessed; 18 19is( $json->encode( $obj ), '[1,2,{"foo":"bar"},"hoge"]' ); 20 21$json->convert_blessed(0); 22 23is( $json->encode( $obj ), 'null' ); 24 25$json->allow_blessed(0)->convert_blessed(1); 26 27is( $json->encode( $obj ), '[1,2,{"foo":"bar"},"hoge"]' ); 28 29} 30 31package Test; 32 33sub new { 34 bless $_[1], $_[0]; 35} 36 37 38 39package Test2; 40 41sub new { 42 bless $_[1], $_[0]; 43} 44 45sub TO_JSON { 46 "hoge"; 47} 48 49