xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/008_pc_base.t (revision f2a19305cfc49ea4d1a5feb55cd6c283c6f1e031)
1898184e3Ssthenuse Test::More;
2898184e3Ssthen
35759b3d2Safresh1# copied over from JSON::PC and modified to use JSON::PP
45759b3d2Safresh1# copied over from JSON::XS and modified to use JSON::PP
5898184e3Ssthen
6898184e3Ssthenuse strict;
7256a93a4Safresh1use warnings;
8898184e3SsthenBEGIN { plan tests => 20 };
9898184e3SsthenBEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
10898184e3Ssthen
11898184e3Ssthenuse JSON::PP;
12898184e3Ssthen
13898184e3Ssthenmy ($js,$obj);
14898184e3Ssthen
15256a93a4Safresh1my $pc = JSON::PP->new;
16898184e3Ssthen
17898184e3Ssthen$js  = q|{}|;
18898184e3Ssthen
19898184e3Ssthen$obj = $pc->decode($js);
20898184e3Ssthen$js  = $pc->encode($obj);
21898184e3Ssthenis($js,'{}', '{}');
22898184e3Ssthen
23898184e3Ssthen$js  = q|[]|;
24898184e3Ssthen$obj = $pc->decode($js);
25898184e3Ssthen$js  = $pc->encode($obj);
26898184e3Ssthenis($js,'[]', '[]');
27898184e3Ssthen
28898184e3Ssthen
29898184e3Ssthen$js  = q|{"foo":"bar"}|;
30898184e3Ssthen$obj = $pc->decode($js);
31898184e3Ssthenis($obj->{foo},'bar');
32898184e3Ssthen$js  = $pc->encode($obj);
33898184e3Ssthenis($js,'{"foo":"bar"}', '{"foo":"bar"}');
34898184e3Ssthen
35898184e3Ssthen$js  = q|{"foo":""}|;
36898184e3Ssthen$obj = $pc->decode($js);
37898184e3Ssthen$js  = $pc->encode($obj);
38898184e3Ssthenis($js,'{"foo":""}', '{"foo":""}');
39898184e3Ssthen
40898184e3Ssthen$js  = q|{"foo":" "}|;
41898184e3Ssthen$obj = $pc->decode($js);
42898184e3Ssthen$js  = $pc->encode($obj);
43898184e3Ssthenis($js,'{"foo":" "}' ,'{"foo":" "}');
44898184e3Ssthen
45898184e3Ssthen$js  = q|{"foo":"0"}|;
46898184e3Ssthen$obj = $pc->decode($js);
47898184e3Ssthen$js  = $pc->encode($obj);
48898184e3Ssthenis($js,'{"foo":"0"}',q|{"foo":"0"} - autoencode (default)|);
49898184e3Ssthen
50898184e3Ssthen
51898184e3Ssthen$js  = q|{"foo":"0 0"}|;
52898184e3Ssthen$obj = $pc->decode($js);
53898184e3Ssthen$js  = $pc->encode($obj);
54898184e3Ssthenis($js,'{"foo":"0 0"}','{"foo":"0 0"}');
55898184e3Ssthen
56898184e3Ssthen$js  = q|[1,2,3]|;
57898184e3Ssthen$obj = $pc->decode($js);
58898184e3Ssthenis($obj->[1],2);
59898184e3Ssthen$js  = $pc->encode($obj);
60898184e3Ssthenis($js,'[1,2,3]');
61898184e3Ssthen
62898184e3Ssthen$js = q|{"foo":{"bar":"hoge"}}|;
63898184e3Ssthen$obj = $pc->decode($js);
64898184e3Ssthenis($obj->{foo}->{bar},'hoge');
65898184e3Ssthen$js  = $pc->encode($obj);
66898184e3Ssthenis($js,q|{"foo":{"bar":"hoge"}}|);
67898184e3Ssthen
68898184e3Ssthen$js = q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|;
69898184e3Ssthen$obj = $pc->decode($js);
70898184e3Ssthen$js  = $pc->encode($obj);
71898184e3Ssthenis($js,q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|);
72898184e3Ssthen
73898184e3Ssthen
74898184e3Ssthen$obj = ["\x01"];
75898184e3Ssthenis($js = $pc->encode($obj),'["\\u0001"]');
76898184e3Ssthen$obj = $pc->decode($js);
77898184e3Ssthenis($obj->[0],"\x01");
78898184e3Ssthen
79898184e3Ssthen$obj = ["\e"];
80*f2a19305Safresh1is($js = $pc->encode($obj), (ord("A") == 65) ? '["\\u001b"]' : '["\\u0027"]');
81898184e3Ssthen$obj = $pc->decode($js);
82898184e3Ssthenis($obj->[0],"\e");
83898184e3Ssthen
84898184e3Ssthen$js = '{"id":"}';
85898184e3Sstheneval q{ $pc->decode($js) };
86898184e3Ssthenlike($@, qr/unexpected end/i);
87898184e3Ssthen
88898184e3Ssthen$obj = { foo => sub { "bar" } };
89898184e3Sstheneval q{ $js = $pc->encode($obj) };
90898184e3Ssthenlike($@, qr/JSON can only/i, 'invalid value (coderef)');
91898184e3Ssthen
92898184e3Ssthen#$obj = { foo => bless {}, "Hoge" };
93898184e3Ssthen#eval q{ $js = $pc->encode($obj) };
94f3efcd01Safresh1#like($@, qr/JSON can only/i, 'invalid value (blessd object)');
95898184e3Ssthen
96898184e3Ssthen$obj = { foo => \$js };
97898184e3Sstheneval q{ $js = $pc->encode($obj) };
98898184e3Ssthenlike($@, qr/cannot encode reference/i, 'invalid value (ref)');
99898184e3Ssthen
100