1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
|
use Test::More;
# copied over from JSON::PC and modified to use JSON::PP
# copied over from JSON::XS and modified to use JSON::PP
use strict;
BEGIN { plan tests => 20 };
BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
use JSON::PP;
my ($js,$obj);
my $pc = new JSON::PP;
$js = q|{}|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'{}', '{}');
$js = q|[]|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'[]', '[]');
$js = q|{"foo":"bar"}|;
$obj = $pc->decode($js);
is($obj->{foo},'bar');
$js = $pc->encode($obj);
is($js,'{"foo":"bar"}', '{"foo":"bar"}');
$js = q|{"foo":""}|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'{"foo":""}', '{"foo":""}');
$js = q|{"foo":" "}|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'{"foo":" "}' ,'{"foo":" "}');
$js = q|{"foo":"0"}|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'{"foo":"0"}',q|{"foo":"0"} - autoencode (default)|);
$js = q|{"foo":"0 0"}|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,'{"foo":"0 0"}','{"foo":"0 0"}');
$js = q|[1,2,3]|;
$obj = $pc->decode($js);
is($obj->[1],2);
$js = $pc->encode($obj);
is($js,'[1,2,3]');
$js = q|{"foo":{"bar":"hoge"}}|;
$obj = $pc->decode($js);
is($obj->{foo}->{bar},'hoge');
$js = $pc->encode($obj);
is($js,q|{"foo":{"bar":"hoge"}}|);
$js = q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|;
$obj = $pc->decode($js);
$js = $pc->encode($obj);
is($js,q|[{"foo":[1,2,3]},-0.12,{"a":"b"}]|);
$obj = ["\x01"];
is($js = $pc->encode($obj),'["\\u0001"]');
$obj = $pc->decode($js);
is($obj->[0],"\x01");
$obj = ["\e"];
is($js = $pc->encode($obj),'["\\u001b"]');
$obj = $pc->decode($js);
is($obj->[0],"\e");
$js = '{"id":"}';
eval q{ $pc->decode($js) };
like($@, qr/unexpected end/i);
$obj = { foo => sub { "bar" } };
eval q{ $js = $pc->encode($obj) };
like($@, qr/JSON can only/i, 'invalid value (coderef)');
#$obj = { foo => bless {}, "Hoge" };
#eval q{ $js = $pc->encode($obj) };
#like($@, qr/JSON::PP can only/i, 'invalid value (blessd object)');
$obj = { foo => \$js };
eval q{ $js = $pc->encode($obj) };
like($@, qr/cannot encode reference/i, 'invalid value (ref)');
|