1# copied over from JSON::XS and modified to use JSON
2
3use strict;
4use Test::More;
5BEGIN { plan tests => 9 };
6
7BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
8
9BEGIN {
10    use lib qw(t);
11    use _unicode_handling;
12}
13
14
15use utf8;
16use JSON;
17
18
19ok (JSON->new->allow_nonref (1)->utf8 (1)->encode ("ü") eq "\"\xc3\xbc\"");
20ok (JSON->new->allow_nonref (1)->encode ("ü") eq "\"ü\"");
21
22SKIP: {
23    skip "UNICODE handling is disabale.", 7 unless $JSON::can_handle_UTF16_and_utf8;
24
25ok (JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->encode (chr 0x8000) eq '"\u8000"');
26ok (JSON->new->allow_nonref (1)->ascii (1)->utf8 (1)->pretty (1)->encode (chr 0x10402) eq "\"\\ud801\\udc02\"\n");
27
28eval { JSON->new->allow_nonref (1)->utf8 (1)->decode ('"ü"') };
29ok $@ =~ /malformed UTF-8/;
30
31ok (JSON->new->allow_nonref (1)->decode ('"ü"') eq "ü");
32ok (JSON->new->allow_nonref (1)->decode ('"\u00fc"') eq "ü");
33ok (JSON->new->allow_nonref (1)->decode ('"\ud801\udc02' . "\x{10204}\"") eq "\x{10402}\x{10204}");
34ok (JSON->new->allow_nonref (1)->decode ('"\"\n\\\\\r\t\f\b"') eq "\"\012\\\015\011\014\010");
35
36}
37