xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/108_decode.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1898184e3Ssthen#
2898184e3Ssthen# decode on Perl 5.005, 5.6, 5.8 or later
3898184e3Ssthen#
4898184e3Ssthenuse strict;
5256a93a4Safresh1use warnings;
6898184e3Ssthenuse Test::More;
7898184e3Ssthen
8*f2a19305Safresh1BEGIN { plan tests => 7 };
9898184e3Ssthen
10898184e3SsthenBEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
11898184e3Ssthen
12*f2a19305Safresh1my $isASCII = ord "A" == 65;
13*f2a19305Safresh1
14898184e3Ssthenuse JSON::PP;
15898184e3Ssthen
16898184e3Ssthenno utf8;
17898184e3Ssthen
18898184e3Ssthenmy $json = JSON::PP->new->allow_nonref;
19898184e3Ssthen
20898184e3Ssthen
21898184e3Ssthenis($json->decode(q|"ü"|),                   "ü"); # utf8
22898184e3Ssthenis($json->decode(q|"\u00fc"|),           "\xfc"); # latin1
23898184e3Ssthenis($json->decode(q|"\u00c3\u00bc"|), "\xc3\xbc"); # utf8
24898184e3Ssthen
25898184e3Ssthenmy $str = 'あ'; # Japanese 'a' in utf8
26898184e3Ssthen
27*f2a19305Safresh1is($json->decode(($isASCII) ? q|"\u00e3\u0081\u0082"|
28*f2a19305Safresh1                            : q|"\u00ce\u0043\u0043"|),
29*f2a19305Safresh1                  $str);
30898184e3Ssthen
31898184e3Ssthenutf8::decode($str); # usually UTF-8 flagged on, but no-op for 5.005.
32898184e3Ssthen
33898184e3Ssthenis($json->decode(q|"\u3042"|), $str);
34898184e3Ssthen
35898184e3Ssthen
36*f2a19305Safresh1# chr 0x12400, which was chosen because it has the same representation in
37*f2a19305Safresh1# both EBCDIC 1047 and 037
38*f2a19305Safresh1my $utf8 = $json->decode(q|"\ud809\udc00"|);
39898184e3Ssthen
405759b3d2Safresh1utf8::encode($utf8); # UTF-8 flagged off
41898184e3Ssthen
42*f2a19305Safresh1is($utf8, ($isASCII) ? "\xf0\x92\x90\x80" : "\xDE\x4A\x41\x41");
43898184e3Ssthen
44*f2a19305Safresh1eval { $json->decode(q|{"action":"foo" "method":"bar","tid":1}|) };
45*f2a19305Safresh1my $error = $@;
46*f2a19305Safresh1like $error => qr!""method":"bar","tid"..."!;
47