1#!perl
2$|++;
3use strict;
4use utf8;
5use Test::More;
6
7eval "use JSON::Any";
8
9if ($@) {
10    plan skip_all => "$@";
11    exit;
12}
13
14$ENV{JSON_ANY_CONFIG} = "utf8=1";
15
16foreach my $backend qw(XS JSON DWIW Syck PC) {
17    my $j = eval {
18        JSON::Any->import($backend);
19        JSON::Any->new;
20    };
21
22    diag "$backend: " . $@ and next if $@;
23
24    $j and $j->handler or next;
25
26    diag "handler is " . ( ref( $j->handler ) || $j->handlerType );
27
28    plan 'no_plan' unless $ENV{JSON_ANY_RAN_TESTS};
29    $ENV{JSON_ANY_RAN_TESTS} = 1;
30
31    foreach my $text qw(foo שלום) {
32
33        my $struct = [$text];
34
35        my $frozen = $j->encode($struct);
36        my $thawed = $j->decode($frozen);
37
38        ok( utf8::is_utf8($frozen) || !scalar( $frozen !~ /[\w\d[:punct:]]/ ),
39            "json output is utf8" );
40
41        is_deeply( $thawed, $struct, "deeply" );
42
43        is( $thawed->[0], $text, "text is the same" ) || eval {
44            require Devel::StringInfo;
45            my $d = Devel::StringInfo->new;
46            $d->dump_info( $text, name => "expected" );
47            $d->dump_info( $thawed->[0], name => "got" );
48            $d->dump_info($frozen);
49        };
50
51        ok( utf8::is_utf8( $thawed->[0] ) || !scalar( $text !~ /[a-z]/ ),
52            "text is utf8 if it needs to be" );
53
54        if ( utf8::valid($frozen) ) {
55            utf8::decode($frozen);
56
57            my $thawed = $j->decode($frozen);
58
59            is_deeply( $thawed, $struct, "deeply" );
60
61            is( $thawed->[0], $text, "text is the same" ) || eval {
62                require Devel::StringInfo;
63                my $d = Devel::StringInfo->new;
64                $d->dump_info( $text, name => "expected" );
65                $d->dump_info( $thawed->[0], name => "got" );
66                $d->dump_info($frozen);
67            };
68
69            ok( utf8::is_utf8( $thawed->[0] ) || !scalar( $text !~ /[a-z]/ ),
70                "text is utf8 if it needs to be" );
71        }
72    }
73}
74
75plan skip_all => 'no JSON package with unicode support installed'
76  unless $ENV{JSON_ANY_RAN_TESTS};
77