1#!perl -T 2 3use utf8; 4use strict; 5use Test::More; 6 7eval "use JSON::Any qw(XS)"; 8if ($@) { 9 plan skip_all => "JSON::XS not installed: $@"; 10} 11else { 12 plan tests => 20; 13} 14 15skip "JSON::XS not installed: $@", 1 if $@; 16diag("Testing JSON::XS backend"); 17 18is( JSON::Any->encode({foo=>'bar'}), qq[{"foo":"bar"}] ); 19 20my ( $json, $js, $obj ); 21ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => 1 ) ); 22is( $json->encode("ü"), qq["ü"] ); 23 24foreach my $bytes ( 0, 1 ) { 25 foreach my $utf8_support ( 0, 1 ) { 26 my $str = "ü"; 27 utf8::encode($str) if $bytes; 28 ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => $utf8_support ) ); 29 30 if ( $utf8_support ) { # only then do we make assumptions about the output json 31 is( $json->encode($str), qq["$str"] ); 32 } 33 34 is( $json->decode($json->encode($str)), $str, "round tripping" ); 35 } 36} 37 38ok( $json = JSON::Any->new( allow_nonref => 1, ascii => 1, utf8 => 1 ) ); 39is( $json->encode( chr 0x8000 ), '"\u8000"' ); 40ok( 41 $json = JSON::Any->new( 42 allow_nonref => 1, 43 ascii => 1, 44 utf8 => 1, 45 pretty => 1 46 ) 47); 48is( $json->encode( chr 0x10402 ), '"\ud801\udc02"' ); 49ok( $json = JSON::Any->new( allow_nonref => 1, utf8 => 1 ) ); 50 51is( $json->encode("ü"), qq["\xc3\xbc\"] ); 52 53is( JSON::Any->encode({foo=>'bar'}), qq[{"foo":"bar"}] ); 54 55