xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/109_encode.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
8898184e3SsthenBEGIN { plan tests => 7 };
9898184e3Ssthen
10898184e3SsthenBEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
11898184e3Ssthen
12898184e3Ssthenuse JSON::PP;
13898184e3Ssthen
14898184e3Ssthenno utf8;
15898184e3Ssthen
16898184e3Ssthenmy $json = JSON::PP->new->allow_nonref;
17898184e3Ssthen
18*f2a19305Safresh1# U+00B6 chosen because it works on both ASCII and EBCDIC
19*f2a19305Safresh1is($json->encode("¶"),                   q|"¶"|); # as is
20898184e3Ssthen
21898184e3Ssthen$json->ascii;
22898184e3Ssthen
23*f2a19305Safresh1is($json->encode("\xb6"),           q|"\u00b6"|); # latin1
24898184e3Ssthen
25*f2a19305Safresh1if (ord "A" == 65)  {
26*f2a19305Safresh1    is($json->encode("\xc2\xb6"), q|"\u00c2\u00b6"|); # utf8
27*f2a19305Safresh1    is($json->encode("¶"),        q|"\u00c2\u00b6"|); # utf8
28*f2a19305Safresh1    is($json->encode('あ'), q|"\u00e3\u0081\u0082"|);
29898184e3Ssthen}
30898184e3Ssthenelse {
31*f2a19305Safresh1    if (ord '^' == 95) {    # EBCDIC 1047
32*f2a19305Safresh1        is($json->encode("\x80\x65"), q|"\u0080\u0065"|); # utf8
33*f2a19305Safresh1        is($json->encode("¶"),        q|"\u0080\u0065"|); # utf8
34*f2a19305Safresh1    }
35*f2a19305Safresh1    else {  # Assume EBCDIC 037
36*f2a19305Safresh1        is($json->encode("\x78\x64"), q|"\u0078\u0064"|); # utf8
37*f2a19305Safresh1        is($json->encode("¶"),        q|"\u0078\u0064"|); # utf8
38898184e3Ssthen    }
39898184e3Ssthen
40*f2a19305Safresh1    is($json->encode('あ'), (q|"\u00ce\u0043\u0043"|));
41*f2a19305Safresh1}
42*f2a19305Safresh1
43*f2a19305Safresh1is($json->encode(chr hex 3042 ),  q|"\u3042"|);
44*f2a19305Safresh1is($json->encode(chr hex 12345 ), q|"\ud808\udf45"|);
45