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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
# use the testsuite from http://www.json.org/JSON_checker/
# except for fail18.json, as we do not support a depth of 20 (but 16 and 32).
# copied over from JSON::XS and modified to use JSON::PP
use strict;
#no warnings;
local $^W = undef;
use Test::More;
BEGIN { plan tests => 39 };
BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
use JSON::PP;
my $json = JSON::PP->new->utf8->max_depth(32)->canonical;
my $vax_float = (pack("d",1) =~ /^[\x80\x10]\x40/);
binmode DATA;
my $num = 1;
for (;;) {
$/ = "\n# ";
chomp (my $test = <DATA>)
or last;
$/ = "\n";
my $name = <DATA>;
if ($vax_float && $name =~ /pass1.json/) {
$test =~ s/\b23456789012E66\b/23456789012E20/;
}
if (my $perl = eval { $json->decode ($test) }) {
ok ($name =~ /^pass/, $name);
#print $json->encode ($perl), "\n";
is ($json->encode ($json->decode ($json->encode ($perl))), $json->encode ($perl));
} else {
ok ($name =~ /^fail/, "$name ($@)");
}
}
__DATA__
"A JSON::PP payload should be an object or array, not a string."
# fail1.json
{"Extra value after close": true} "misplaced quoted value"
# fail10.json
{"Illegal expression": 1 + 2}
# fail11.json
{"Illegal invocation": alert()}
# fail12.json
{"Numbers cannot have leading zeroes": 013}
# fail13.json
{"Numbers cannot be hex": 0x14}
# fail14.json
["Illegal backslash escape: \x15"]
# fail15.json
[\naked]
# fail16.json
["Illegal backslash escape: \017"]
# fail17.json
[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
# fail18.json
{"Missing colon" null}
# fail19.json
["Unclosed array"
# fail2.json
{"Double colon":: null}
# fail20.json
{"Comma instead of colon", null}
# fail21.json
["Colon instead of comma": false]
# fail22.json
["Bad value", truth]
# fail23.json
['single quote']
# fail24.json
[" tab character in string "]
# fail25.json
["tab\ character\ in\ string\ "]
# fail26.json
["line
break"]
# fail27.json
["line\
break"]
# fail28.json
[0e]
# fail29.json
{unquoted_key: "keys must be quoted"}
# fail3.json
[0e+]
# fail30.json
[0e+-1]
# fail31.json
{"Comma instead if closing brace": true,
# fail32.json
["mismatch"}
# fail33.json
["extra comma",]
# fail4.json
["double extra comma",,]
# fail5.json
[ , "<-- missing value"]
# fail6.json
["Comma after the close"],
# fail7.json
["Extra close"]]
# fail8.json
{"Extra comma": true,}
# fail9.json
[
"JSON::PP Test Pattern pass1",
{"object with 1 member":["array with 1 element"]},
{},
[],
-42,
true,
false,
null,
{
"integer": 1234567890,
"real": -9876.543210,
"e": 0.123456789e-12,
"E": 1.234567890E+34,
"": 23456789012E66,
"zero": 0,
"one": 1,
"space": " ",
"quote": "\"",
"backslash": "\\",
"controls": "\b\f\n\r\t",
"slash": "/ & \/",
"alpha": "abcdefghijklmnopqrstuvwyz",
"ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
"digit": "0123456789",
"0123456789": "digit",
"special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
"hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
"true": true,
"false": false,
"null": null,
"array":[ ],
"object":{ },
"address": "50 St. James Street",
"url": "http://www.JSON::PP.org/",
"comment": "// /* <!-- --",
"# -- --> */": " ",
" s p a c e d " :[1,2 , 3
,
4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
"jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
"quotes": "" \u0022 %22 0x22 034 "",
"\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
: "A key can be any string"
},
0.5 ,98.6
,
99.44
,
1066,
1e1,
0.1e1,
1e-1,
1e00,2e+00,2e-00
,"rosebud"]
# pass1.json
[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
# pass2.json
{
"JSON::PP Test Pattern pass3": {
"The outermost value": "must be an object or array.",
"In this test": "It is an object."
}
}
# pass3.json
|