xref: /openbsd-src/gnu/usr.bin/perl/cpan/JSON-PP/t/006_pc_pretty.t (revision 256a93a44f36679bee503f12e49566c2183f6181)
15759b3d2Safresh1# copied over from JSON::PC and modified to use JSON::PP
25759b3d2Safresh1# copied over from JSON::XS and modified to use JSON::PP
3898184e3Ssthen
4898184e3Ssthenuse strict;
5*256a93a4Safresh1use warnings;
6898184e3Ssthenuse Test::More;
7898184e3SsthenBEGIN { plan tests => 9 };
8898184e3Ssthen
9898184e3SsthenBEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
10898184e3Ssthen
11898184e3Ssthenuse JSON::PP;
12898184e3Ssthen
13898184e3Ssthenmy ($js,$obj,$json);
14*256a93a4Safresh1my $pc = JSON::PP->new;
15898184e3Ssthen
16898184e3Ssthen$obj = {foo => "bar"};
17898184e3Ssthen$js = $pc->encode($obj);
18898184e3Ssthenis($js,q|{"foo":"bar"}|);
19898184e3Ssthen
20898184e3Ssthen$obj = [10, "hoge", {foo => "bar"}];
21898184e3Ssthen$pc->pretty (1);
22898184e3Ssthen$js = $pc->encode($obj);
23898184e3Ssthenis($js,q|[
24898184e3Ssthen   10,
25898184e3Ssthen   "hoge",
26898184e3Ssthen   {
27898184e3Ssthen      "foo" : "bar"
28898184e3Ssthen   }
29898184e3Ssthen]
30898184e3Ssthen|);
31898184e3Ssthen
32898184e3Ssthen$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
33898184e3Ssthen$pc->pretty(0);
34898184e3Ssthen$js = $pc->encode($obj);
35898184e3Ssthenis($js,q|{"foo":[{"a":"b"},0,1,2]}|);
36898184e3Ssthen
37898184e3Ssthen
38898184e3Ssthen$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
39898184e3Ssthen$pc->pretty(1);
40898184e3Ssthen$js = $pc->encode($obj);
41898184e3Ssthenis($js,q|{
42898184e3Ssthen   "foo" : [
43898184e3Ssthen      {
44898184e3Ssthen         "a" : "b"
45898184e3Ssthen      },
46898184e3Ssthen      0,
47898184e3Ssthen      1,
48898184e3Ssthen      2
49898184e3Ssthen   ]
50898184e3Ssthen}
51898184e3Ssthen|);
52898184e3Ssthen
53898184e3Ssthen$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
54898184e3Ssthen$pc->pretty(0);
55898184e3Ssthen$js = $pc->encode($obj);
56898184e3Ssthenis($js,q|{"foo":[{"a":"b"},0,1,2]}|);
57898184e3Ssthen
58898184e3Ssthen
59898184e3Ssthen$obj = {foo => "bar"};
60f3efcd01Safresh1$pc->indent(1);
61898184e3Ssthenis($pc->encode($obj), qq|{\n   "foo":"bar"\n}\n|, "nospace");
62898184e3Ssthen$pc->space_after(1);
63898184e3Ssthenis($pc->encode($obj), qq|{\n   "foo": "bar"\n}\n|, "after");
64898184e3Ssthen$pc->space_before(1);
65898184e3Ssthenis($pc->encode($obj), qq|{\n   "foo" : "bar"\n}\n|, "both");
66898184e3Ssthen$pc->space_after(0);
67898184e3Ssthenis($pc->encode($obj), qq|{\n   "foo" :"bar"\n}\n|, "before");
68898184e3Ssthen
69