1#! perl
2
3# copied over from JSON::PC and modified to use Cpanel::JSON::XS
4
5use strict;
6use Test::More;
7BEGIN { plan tests => 10 };
8
9use Cpanel::JSON::XS;
10
11my ($js,$obj,$json);
12my $pc = new Cpanel::JSON::XS;
13
14$obj = {foo => "bar"};
15$js = $pc->encode($obj);
16is($js,q|{"foo":"bar"}|);
17
18$obj = [10, "hoge", {foo => "bar"}];
19$pc->pretty (1);
20$js = $pc->encode($obj);
21is($js,q|[
22   10,
23   "hoge",
24   {
25      "foo" : "bar"
26   }
27]
28|);
29
30$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
31$pc->pretty(0);
32$js = $pc->encode($obj);
33is($js,q|{"foo":[{"a":"b"},0,1,2]}|);
34
35
36$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
37$pc->pretty(1);
38$js = $pc->encode($obj);
39is($js,q|{
40   "foo" : [
41      {
42         "a" : "b"
43      },
44      0,
45      1,
46      2
47   ]
48}
49|);
50
51$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
52$pc->pretty(0);
53$js = $pc->encode($obj);
54is($js,q|{"foo":[{"a":"b"},0,1,2]}|);
55
56
57$obj = {foo => "bar"};
58$pc->indent(1);
59is($pc->encode($obj), qq|{\n   "foo":"bar"\n}\n|, "nospace");
60$pc->space_after(1);
61is($pc->encode($obj), qq|{\n   "foo": "bar"\n}\n|, "after");
62$pc->space_before(1);
63is($pc->encode($obj), qq|{\n   "foo" : "bar"\n}\n|, "both");
64$pc->space_after(0);
65is($pc->encode($obj), qq|{\n   "foo" :"bar"\n}\n|, "before");
66
67$obj = { foo => [ {a=>"b"}, 0, 1, 2 ] };
68$pc->pretty(1)->indent_length(1);
69$js = $pc->encode($obj);
70is($js,q|{
71 "foo" : [
72  {
73   "a" : "b"
74  },
75  0,
76  1,
77  2
78 ]
79}
80|);
81
82