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