1#! perl
2
3# use the testsuite from http://www.json.org/JSON_checker/
4# except for fail18.json, as we do not support a depth of 20 (but 16 and 32).
5
6# copied over from JSON::XS and modified to use JSON
7
8use strict;
9#no warnings;
10local $^W = undef;
11use Test::More;
12BEGIN { plan tests => 39 };
13BEGIN { $ENV{PERL_JSON_BACKEND} = 0; }
14
15use JSON;
16
17my $json = JSON->new->utf8->max_depth(32)->canonical;
18
19binmode DATA;
20my $num = 1;
21for (;;) {
22
23   $/ = "\n# ";
24   chomp (my $test = <DATA>)
25      or last;
26   $/ = "\n";
27   my $name = <DATA>;
28   if (my $perl = eval { $json->decode ($test) }) {
29      ok ($name =~ /^pass/, $name);
30#print $json->encode ($perl), "\n";
31      is ($json->encode ($json->decode ($json->encode ($perl))), $json->encode ($perl));
32   } else {
33      ok ($name =~ /^fail/, "$name ($@)");
34   }
35
36}
37
38__DATA__
39"A JSON payload should be an object or array, not a string."
40# fail1.json
41{"Extra value after close": true} "misplaced quoted value"
42# fail10.json
43{"Illegal expression": 1 + 2}
44# fail11.json
45{"Illegal invocation": alert()}
46# fail12.json
47{"Numbers cannot have leading zeroes": 013}
48# fail13.json
49{"Numbers cannot be hex": 0x14}
50# fail14.json
51["Illegal backslash escape: \x15"]
52# fail15.json
53[\naked]
54# fail16.json
55["Illegal backslash escape: \017"]
56# fail17.json
57[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[["Too deep"]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]
58# fail18.json
59{"Missing colon" null}
60# fail19.json
61["Unclosed array"
62# fail2.json
63{"Double colon":: null}
64# fail20.json
65{"Comma instead of colon", null}
66# fail21.json
67["Colon instead of comma": false]
68# fail22.json
69["Bad value", truth]
70# fail23.json
71['single quote']
72# fail24.json
73["	tab	character	in	string	"]
74# fail25.json
75["tab\   character\   in\  string\  "]
76# fail26.json
77["line
78break"]
79# fail27.json
80["line\
81break"]
82# fail28.json
83[0e]
84# fail29.json
85{unquoted_key: "keys must be quoted"}
86# fail3.json
87[0e+]
88# fail30.json
89[0e+-1]
90# fail31.json
91{"Comma instead if closing brace": true,
92# fail32.json
93["mismatch"}
94# fail33.json
95["extra comma",]
96# fail4.json
97["double extra comma",,]
98# fail5.json
99[   , "<-- missing value"]
100# fail6.json
101["Comma after the close"],
102# fail7.json
103["Extra close"]]
104# fail8.json
105{"Extra comma": true,}
106# fail9.json
107[
108    "JSON Test Pattern pass1",
109    {"object with 1 member":["array with 1 element"]},
110    {},
111    [],
112    -42,
113    true,
114    false,
115    null,
116    {
117        "integer": 1234567890,
118        "real": -9876.543210,
119        "e": 0.123456789e-12,
120        "E": 1.234567890E+34,
121        "":  23456789012E66,
122        "zero": 0,
123        "one": 1,
124        "space": " ",
125        "quote": "\"",
126        "backslash": "\\",
127        "controls": "\b\f\n\r\t",
128        "slash": "/ & \/",
129        "alpha": "abcdefghijklmnopqrstuvwyz",
130        "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
131        "digit": "0123456789",
132        "0123456789": "digit",
133        "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
134        "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
135        "true": true,
136        "false": false,
137        "null": null,
138        "array":[  ],
139        "object":{  },
140        "address": "50 St. James Street",
141        "url": "http://www.JSON.org/",
142        "comment": "// /* <!-- --",
143        "# -- --> */": " ",
144        " s p a c e d " :[1,2 , 3
145
146,
147
1484 , 5        ,          6           ,7        ],"compact":[1,2,3,4,5,6,7],
149        "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
150        "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
151        "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
152: "A key can be any string"
153    },
154    0.5 ,98.6
155,
15699.44
157,
158
1591066,
1601e1,
1610.1e1,
1621e-1,
1631e00,2e+00,2e-00
164,"rosebud"]
165# pass1.json
166[[[[[[[[[[[[[[[[[[["Not too deep"]]]]]]]]]]]]]]]]]]]
167# pass2.json
168{
169    "JSON Test Pattern pass3": {
170        "The outermost value": "must be an object or array.",
171        "In this test": "It is an object."
172    }
173}
174
175# pass3.json
176