| 1 |
|
| 2 |
use strict; |
| 3 |
use Test::More; |
| 4 |
BEGIN { plan tests => 7 }; |
| 5 |
|
| 6 |
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } |
| 7 |
|
| 8 |
use JSON -convert_blessed_universally; |
| 9 |
|
| 10 |
ok( !MyTest->can('TO_JSON') ); |
| 11 |
ok( MyTest2->can('TO_JSON') ); |
| 12 |
|
| 13 |
my $obj = MyTest->new( [ 1, 2, {foo => 'bar'} ] ); |
| 14 |
|
| 15 |
$obj->[3] = MyTest2->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 |
SKIP: { |
| 30 |
skip "only works with 5.18+", 1 if $] < 5.018; |
| 31 |
ok( !MyTest->can('TO_JSON') ); |
| 32 |
} |
| 33 |
ok( MyTest2->can('TO_JSON') ); |
| 34 |
|
| 35 |
package MyTest; |
| 36 |
|
| 37 |
sub new { |
| 38 |
bless $_[1], $_[0]; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
package MyTest2; |
| 44 |
|
| 45 |
sub new { |
| 46 |
bless $_[1], $_[0]; |
| 47 |
} |
| 48 |
|
| 49 |
sub TO_JSON { |
| 50 |
"hoge"; |
| 51 |
} |
| 52 |
|