3 * Small test for the minijson encoder/decoder routines
6 * Thorsten Glaser <mirabilos@evolvis.org>
9 * This file is part of FusionForge. FusionForge is free software;
10 * you can redistribute it and/or modify it under the terms of the
11 * GNU General Public License as published by the Free Software
12 * Foundation; either version 2 of the Licence, or (at your option)
15 * FusionForge is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with FusionForge; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 /****************************************************************/
29 /* $s_orig [parse] print_r->$s_printr [encode] $s_ecompact or $s_epadded */
31 /* $s_e* [decode] print_r ->$s_printr */
34 "JSON Test Pattern pass1",
35 {"object with 1 member":["array with 1 element"]},
43 "integer": 1234567890,
53 "controls": "\\b\\f\\n\\r\\t",
55 "alpha": "abcdefghijklmnopqrstuvwyz",
56 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
57 "digit": "0123456789",
58 "0123456789": "digit",
59 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
60 "hex": "\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A",
66 "address": "50 St. James Street",
67 "url": "http://www.JSON.org/",
68 "comment": "// /* <!-- --",
70 " s p a c e d " :[1,2 , 3
74 4 , 5 , 6 ,7 ],"compact":[1,2,3,4,5,6,7],
75 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
76 "quotes": "" \\u0022 %22 0x22 034 "",
77 "\\/\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?"
78 : "A key can be any string"
94 [0] => JSON Test Pattern pass1
97 [object with 1 member] => Array
99 [0] => array with 1 element
118 [integer] => 1234567890
119 [real] => -9876.54321
120 [e] => 1.23456789E-13
121 [E] => 1.23456789E+34
122 [] => 2.3456789012E+76
131 [alpha] => abcdefghijklmnopqrstuvwyz
132 [ALPHA] => ABCDEFGHIJKLMNOPQRSTUVWYZ
133 [digit] => 0123456789
134 [0123456789] => digit
135 [special] => `1~!@#$%^&*()_+-={\':[,]}|;.</>?
148 [address] => 50 St. James Street
149 [url] => http://www.JSON.org/
150 [comment] => // /* <!-- --
152 [ s p a c e d ] => Array
174 [jsontext] => {"object with 1 member":["array with 1 element"]}
175 [quotes] => " " %22 0x22 034 "
177 \r `1~!@#$%^&*()_+-=[]{}|;:\',./<>?] => A key can be any string
194 $s_ecompact = '["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},[],[],-42,true,false,null,{"integer":1234567890,"real":-9.87654321E+3,"e":1.23456789E-13,"E":1.23456789E+34,"":2.3456789012E+76,"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":"ģ䕧覫췯ꯍ","true":true,"false":false,"null":null,"array":[],"object":[],"address":"50 St. James Street","url":"http://www.JSON.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":"" \\" %22 0x22 034 "","/\\\\\\"쫾몾ꮘﳞ볚\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?":"A key can be any string"},5.0E-1,9.86E+1,9.944E+1,1066,1.0E+1,1.0,1.0E-1,1.0,2.0,2.0,"rosebud"]';
196 "JSON Test Pattern pass1",
198 "object with 1 member": [
199 "array with 1 element"
213 "integer": 1234567890,
214 "real": -9.87654321E+3,
217 "": 2.3456789012E+76,
223 "controls": "\\b\\f\\n\\r\\t",
225 "alpha": "abcdefghijklmnopqrstuvwyz",
226 "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
227 "digit": "0123456789",
228 "0123456789": "digit",
229 "special": "`1~!@#$%^&*()_+-={\':[,]}|;.</>?",
240 "address": "50 St. James Street",
241 "url": "http://www.JSON.org/",
242 "comment": "// /* <!-- --",
262 "jsontext": "{\\"object with 1 member\\":[\\"array with 1 element\\"]}",
263 "quotes": "" \\" %22 0x22 034 "",
264 "/\\\\\\"쫾몾ꮘﳞ볚\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:\',./<>?": "A key can be any string"
279 /****************************************************************/
281 require_once(dirname(__FILE__) . "/minijson.php");
287 function test_r($label,$ok) {
288 global $tnum, $passed, $failed;
291 echo "PASS " . ++$tnum . "-" . $label . "\n";
295 echo "FAIL " . ++$tnum . "-" . $label . "\n";
301 function test_eq($label,$want,$got) {
303 return test_r($label, true);
304 test_r($label, false);
314 $presult = minijson_decode($s_orig, $parsed);
315 if (test_eq("can-parse", true, $presult)) {
320 if (!$parsed || $parsed == 'bla')
321 echo "FAIL parse-basic\n";
322 $printrd = print_r($parsed, true);
323 test_eq("parsed", $s_printr, $printrd);
325 $encoded = minijson_encode($parsed, false);
326 test_eq("encode-compact", $s_ecompact, $encoded);
328 $presult = minijson_decode($encoded, $reparsed);
329 test_eq("can-reparse-compact", true, $presult);
330 if (!$reparsed || $reparsed == 'bla')
331 echo "FAIL reparse-compact-basic\n";
332 $printrd = print_r($reparsed, true);
333 test_eq("reparsed-compact", $s_printr, $printrd);
335 $encoded = minijson_encode($parsed);
336 test_eq("encode-padded", $s_epadded, $encoded);
338 $presult = minijson_decode($encoded, $reparsed);
339 test_eq("can-reparse-padded", true, $presult);
340 if (!$reparsed || $reparsed == 'bla')
341 echo "FAIL reparse-padded-basic\n";
342 $printrd = print_r($reparsed, true);
343 test_eq("reparsed-padded", $s_printr, $printrd);
345 echo "Total failed: " . $failed . "/" . $tnum . "\n";
346 echo "Total passed: " . $passed . "/" . $tnum . "\n";
347 exit ($failed == 0 ? 0 : 1);