| 1 |
# |
| 2 |
# decode on Perl 5.005, 5.6, 5.8 or later |
| 3 |
# |
| 4 |
use strict; |
| 5 |
use warnings; |
| 6 |
use Test::More; |
| 7 |
|
| 8 |
BEGIN { plan tests => 7 }; |
| 9 |
|
| 10 |
BEGIN { $ENV{PERL_JSON_BACKEND} ||= "JSON::backportPP"; } |
| 11 |
|
| 12 |
my $isASCII = ord "A" == 65; |
| 13 |
|
| 14 |
use JSON; |
| 15 |
|
| 16 |
no utf8; |
| 17 |
|
| 18 |
my $json = JSON->new->allow_nonref; |
| 19 |
|
| 20 |
|
| 21 |
is($json->decode(q|"ü"|), "ü"); # utf8 |
| 22 |
is($json->decode(q|"\u00fc"|), "\xfc"); # latin1 |
| 23 |
is($json->decode(q|"\u00c3\u00bc"|), "\xc3\xbc"); # utf8 |
| 24 |
|
| 25 |
my $str = 'あ'; # Japanese 'a' in utf8 |
| 26 |
|
| 27 |
is($json->decode(($isASCII) ? q|"\u00e3\u0081\u0082"| |
| 28 |
: q|"\u00ce\u0043\u0043"|), |
| 29 |
$str); |
| 30 |
|
| 31 |
utf8::decode($str); # usually UTF-8 flagged on, but no-op for 5.005. |
| 32 |
|
| 33 |
is($json->decode(q|"\u3042"|), $str); |
| 34 |
|
| 35 |
|
| 36 |
# chr 0x12400, which was chosen because it has the same representation in |
| 37 |
# both EBCDIC 1047 and 037 |
| 38 |
my $utf8 = $json->decode(q|"\ud809\udc00"|); |
| 39 |
|
| 40 |
utf8::encode($utf8); # UTF-8 flagged off |
| 41 |
|
| 42 |
is($utf8, ($isASCII) ? "\xf0\x92\x90\x80" : "\xDE\x4A\x41\x41"); |
| 43 |
|
| 44 |
eval { $json->decode(q|{"action":"foo" "method":"bar","tid":1}|) }; |
| 45 |
my $error = $@; |
| 46 |
like $error => qr!""method":"bar","tid"..."!; |