xref: /PHP-5.3/ext/json/tests/pass001.phpt (revision 250393f9)
1--TEST--
2JSON (http://www.crockford.com/JSON/JSON_checker/test/pass1.json)
3--INI--
4precision=14
5--SKIPIF--
6<?php
7  if (!extension_loaded('json')) die('skip: json extension not available');
8?>
9--FILE--
10<?php
11// Expect warnings about INF.
12ini_set("error_reporting", E_ALL & ~E_WARNING);
13
14$test = "
15[
16    \"JSON Test Pattern pass1\",
17    {\"object with 1 member\":[\"array with 1 element\"]},
18    {},
19    [],
20    -42,
21    true,
22    false,
23    null,
24    {
25        \"integer\": 1234567890,
26        \"real\": -9876.543210,
27        \"e\": 0.123456789e-12,
28        \"E\": 1.234567890E+34,
29        \"\":  23456789012E666,
30        \"zero\": 0,
31        \"one\": 1,
32        \"space\": \" \",
33        \"quote\": \"\\\"\",
34        \"backslash\": \"\\\\\",
35        \"controls\": \"\\b\\f\\n\\r\\t\",
36        \"slash\": \"/ & \\/\",
37        \"alpha\": \"abcdefghijklmnopqrstuvwyz\",
38        \"ALPHA\": \"ABCDEFGHIJKLMNOPQRSTUVWYZ\",
39        \"digit\": \"0123456789\",
40        \"special\": \"`1~!@#$%^&*()_+-={':[,]}|;.</>?\",
41        \"hex\": \"\\u0123\\u4567\\u89AB\\uCDEF\\uabcd\\uef4A\",
42        \"true\": true,
43        \"false\": false,
44        \"null\": null,
45        \"array\":[  ],
46        \"object\":{  },
47        \"address\": \"50 St. James Street\",
48        \"url\": \"http://www.JSON.org/\",
49        \"comment\": \"// /* <!-- --\",
50        \"# -- --> */\": \" \",
51        \" s p a c e d \" :[1,2 , 3
52
53,
54
554 , 5        ,          6           ,7        ],
56        \"compact\": [1,2,3,4,5,6,7],
57        \"jsontext\": \"{\\\"object with 1 member\\\":[\\\"array with 1 element\\\"]}\",
58        \"quotes\": \"&#34; \\u0022 %22 0x22 034 &#x22;\",
59        \"\\/\\\\\\\"\\uCAFE\\uBABE\\uAB98\\uFCDE\\ubcda\\uef4A\\b\\f\\n\\r\\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?\"
60: \"A key can be any string\"
61    },
62    0.5 ,98.6
63,
6499.44
65,
66
671066
68
69
70,\"rosebud\"]
71";
72
73echo 'Testing: ' . $test . "\n";
74echo "DECODE: AS OBJECT\n";
75$obj = json_decode($test);
76var_dump($obj);
77echo "DECODE: AS ARRAY\n";
78$arr = json_decode($test, true);
79var_dump($arr);
80
81echo "ENCODE: FROM OBJECT\n";
82$obj_enc = json_encode($obj);
83echo $obj_enc . "\n";
84echo "ENCODE: FROM ARRAY\n";
85$arr_enc = json_encode($arr);
86echo $arr_enc . "\n";
87
88echo "DECODE AGAIN: AS OBJECT\n";
89$obj = json_decode($obj_enc);
90var_dump($obj);
91echo "DECODE AGAIN: AS ARRAY\n";
92$arr = json_decode($arr_enc, true);
93var_dump($arr);
94
95?>
96--EXPECT--
97Testing:
98[
99    "JSON Test Pattern pass1",
100    {"object with 1 member":["array with 1 element"]},
101    {},
102    [],
103    -42,
104    true,
105    false,
106    null,
107    {
108        "integer": 1234567890,
109        "real": -9876.543210,
110        "e": 0.123456789e-12,
111        "E": 1.234567890E+34,
112        "":  23456789012E666,
113        "zero": 0,
114        "one": 1,
115        "space": " ",
116        "quote": "\"",
117        "backslash": "\\",
118        "controls": "\b\f\n\r\t",
119        "slash": "/ & \/",
120        "alpha": "abcdefghijklmnopqrstuvwyz",
121        "ALPHA": "ABCDEFGHIJKLMNOPQRSTUVWYZ",
122        "digit": "0123456789",
123        "special": "`1~!@#$%^&*()_+-={':[,]}|;.</>?",
124        "hex": "\u0123\u4567\u89AB\uCDEF\uabcd\uef4A",
125        "true": true,
126        "false": false,
127        "null": null,
128        "array":[  ],
129        "object":{  },
130        "address": "50 St. James Street",
131        "url": "http://www.JSON.org/",
132        "comment": "// /* <!-- --",
133        "# -- --> */": " ",
134        " s p a c e d " :[1,2 , 3
135
136,
137
1384 , 5        ,          6           ,7        ],
139        "compact": [1,2,3,4,5,6,7],
140        "jsontext": "{\"object with 1 member\":[\"array with 1 element\"]}",
141        "quotes": "&#34; \u0022 %22 0x22 034 &#x22;",
142        "\/\\\"\uCAFE\uBABE\uAB98\uFCDE\ubcda\uef4A\b\f\n\r\t`1~!@#$%^&*()_+-=[]{}|;:',./<>?"
143: "A key can be any string"
144    },
145    0.5 ,98.6
146,
14799.44
148,
149
1501066
151
152
153,"rosebud"]
154
155DECODE: AS OBJECT
156array(14) {
157  [0]=>
158  string(23) "JSON Test Pattern pass1"
159  [1]=>
160  object(stdClass)#1 (1) {
161    ["object with 1 member"]=>
162    array(1) {
163      [0]=>
164      string(20) "array with 1 element"
165    }
166  }
167  [2]=>
168  object(stdClass)#2 (0) {
169  }
170  [3]=>
171  array(0) {
172  }
173  [4]=>
174  int(-42)
175  [5]=>
176  bool(true)
177  [6]=>
178  bool(false)
179  [7]=>
180  NULL
181  [8]=>
182  object(stdClass)#3 (31) {
183    ["integer"]=>
184    int(1234567890)
185    ["real"]=>
186    float(-9876.54321)
187    ["e"]=>
188    float(1.23456789E-13)
189    ["E"]=>
190    float(1.23456789E+34)
191    ["_empty_"]=>
192    float(INF)
193    ["zero"]=>
194    int(0)
195    ["one"]=>
196    int(1)
197    ["space"]=>
198    string(1) " "
199    ["quote"]=>
200    string(1) """
201    ["backslash"]=>
202    string(1) "\"
203    ["controls"]=>
204    string(5) "
205
205	"
206    ["slash"]=>
207    string(5) "/ & /"
208    ["alpha"]=>
209    string(25) "abcdefghijklmnopqrstuvwyz"
210    ["ALPHA"]=>
211    string(25) "ABCDEFGHIJKLMNOPQRSTUVWYZ"
212    ["digit"]=>
213    string(10) "0123456789"
214    ["special"]=>
215    string(31) "`1~!@#$%^&*()_+-={':[,]}|;.</>?"
216    ["hex"]=>
217    string(17) "ģ䕧覫췯ꯍ"
218    ["true"]=>
219    bool(true)
220    ["false"]=>
221    bool(false)
222    ["null"]=>
223    NULL
224    ["array"]=>
225    array(0) {
226    }
227    ["object"]=>
228    object(stdClass)#4 (0) {
229    }
230    ["address"]=>
231    string(19) "50 St. James Street"
232    ["url"]=>
233    string(20) "http://www.JSON.org/"
234    ["comment"]=>
235    string(13) "// /* <!-- --"
236    ["# -- --> */"]=>
237    string(1) " "
238    [" s p a c e d "]=>
239    array(7) {
240      [0]=>
241      int(1)
242      [1]=>
243      int(2)
244      [2]=>
245      int(3)
246      [3]=>
247      int(4)
248      [4]=>
249      int(5)
250      [5]=>
251      int(6)
252      [6]=>
253      int(7)
254    }
255    ["compact"]=>
256    array(7) {
257      [0]=>
258      int(1)
259      [1]=>
260      int(2)
261      [2]=>
262      int(3)
263      [3]=>
264      int(4)
265      [4]=>
266      int(5)
267      [5]=>
268      int(6)
269      [6]=>
270      int(7)
271    }
272    ["jsontext"]=>
273    string(49) "{"object with 1 member":["array with 1 element"]}"
274    ["quotes"]=>
275    string(27) "&#34; " %22 0x22 034 &#x22;"
276    ["/\"쫾몾ꮘﳞ볚
277
277	`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]=>
278    string(23) "A key can be any string"
279  }
280  [9]=>
281  float(0.5)
282  [10]=>
283  float(98.6)
284  [11]=>
285  float(99.44)
286  [12]=>
287  int(1066)
288  [13]=>
289  string(7) "rosebud"
290}
291DECODE: AS ARRAY
292array(14) {
293  [0]=>
294  string(23) "JSON Test Pattern pass1"
295  [1]=>
296  array(1) {
297    ["object with 1 member"]=>
298    array(1) {
299      [0]=>
300      string(20) "array with 1 element"
301    }
302  }
303  [2]=>
304  array(0) {
305  }
306  [3]=>
307  array(0) {
308  }
309  [4]=>
310  int(-42)
311  [5]=>
312  bool(true)
313  [6]=>
314  bool(false)
315  [7]=>
316  NULL
317  [8]=>
318  array(31) {
319    ["integer"]=>
320    int(1234567890)
321    ["real"]=>
322    float(-9876.54321)
323    ["e"]=>
324    float(1.23456789E-13)
325    ["E"]=>
326    float(1.23456789E+34)
327    [""]=>
328    float(INF)
329    ["zero"]=>
330    int(0)
331    ["one"]=>
332    int(1)
333    ["space"]=>
334    string(1) " "
335    ["quote"]=>
336    string(1) """
337    ["backslash"]=>
338    string(1) "\"
339    ["controls"]=>
340    string(5) "
341
341	"
342    ["slash"]=>
343    string(5) "/ & /"
344    ["alpha"]=>
345    string(25) "abcdefghijklmnopqrstuvwyz"
346    ["ALPHA"]=>
347    string(25) "ABCDEFGHIJKLMNOPQRSTUVWYZ"
348    ["digit"]=>
349    string(10) "0123456789"
350    ["special"]=>
351    string(31) "`1~!@#$%^&*()_+-={':[,]}|;.</>?"
352    ["hex"]=>
353    string(17) "ģ䕧覫췯ꯍ"
354    ["true"]=>
355    bool(true)
356    ["false"]=>
357    bool(false)
358    ["null"]=>
359    NULL
360    ["array"]=>
361    array(0) {
362    }
363    ["object"]=>
364    array(0) {
365    }
366    ["address"]=>
367    string(19) "50 St. James Street"
368    ["url"]=>
369    string(20) "http://www.JSON.org/"
370    ["comment"]=>
371    string(13) "// /* <!-- --"
372    ["# -- --> */"]=>
373    string(1) " "
374    [" s p a c e d "]=>
375    array(7) {
376      [0]=>
377      int(1)
378      [1]=>
379      int(2)
380      [2]=>
381      int(3)
382      [3]=>
383      int(4)
384      [4]=>
385      int(5)
386      [5]=>
387      int(6)
388      [6]=>
389      int(7)
390    }
391    ["compact"]=>
392    array(7) {
393      [0]=>
394      int(1)
395      [1]=>
396      int(2)
397      [2]=>
398      int(3)
399      [3]=>
400      int(4)
401      [4]=>
402      int(5)
403      [5]=>
404      int(6)
405      [6]=>
406      int(7)
407    }
408    ["jsontext"]=>
409    string(49) "{"object with 1 member":["array with 1 element"]}"
410    ["quotes"]=>
411    string(27) "&#34; " %22 0x22 034 &#x22;"
412    ["/\"쫾몾ꮘﳞ볚
413
413	`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]=>
414    string(23) "A key can be any string"
415  }
416  [9]=>
417  float(0.5)
418  [10]=>
419  float(98.6)
420  [11]=>
421  float(99.44)
422  [12]=>
423  int(1066)
424  [13]=>
425  string(7) "rosebud"
426}
427ENCODE: FROM OBJECT
428["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},{},[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"_empty_":0,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","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.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":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\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,"rosebud"]
429ENCODE: FROM ARRAY
430["JSON Test Pattern pass1",{"object with 1 member":["array with 1 element"]},[],[],-42,true,false,null,{"integer":1234567890,"real":-9876.54321,"e":1.23456789e-13,"E":1.23456789e+34,"":0,"zero":0,"one":1,"space":" ","quote":"\"","backslash":"\\","controls":"\b\f\n\r\t","slash":"\/ & \/","alpha":"abcdefghijklmnopqrstuvwyz","ALPHA":"ABCDEFGHIJKLMNOPQRSTUVWYZ","digit":"0123456789","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.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":"&#34; \" %22 0x22 034 &#x22;","\/\\\"\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,"rosebud"]
431DECODE AGAIN: AS OBJECT
432array(14) {
433  [0]=>
434  string(23) "JSON Test Pattern pass1"
435  [1]=>
436  object(stdClass)#5 (1) {
437    ["object with 1 member"]=>
438    array(1) {
439      [0]=>
440      string(20) "array with 1 element"
441    }
442  }
443  [2]=>
444  object(stdClass)#6 (0) {
445  }
446  [3]=>
447  array(0) {
448  }
449  [4]=>
450  int(-42)
451  [5]=>
452  bool(true)
453  [6]=>
454  bool(false)
455  [7]=>
456  NULL
457  [8]=>
458  object(stdClass)#7 (31) {
459    ["integer"]=>
460    int(1234567890)
461    ["real"]=>
462    float(-9876.54321)
463    ["e"]=>
464    float(1.23456789E-13)
465    ["E"]=>
466    float(1.23456789E+34)
467    ["_empty_"]=>
468    int(0)
469    ["zero"]=>
470    int(0)
471    ["one"]=>
472    int(1)
473    ["space"]=>
474    string(1) " "
475    ["quote"]=>
476    string(1) """
477    ["backslash"]=>
478    string(1) "\"
479    ["controls"]=>
480    string(5) "
481
481	"
482    ["slash"]=>
483    string(5) "/ & /"
484    ["alpha"]=>
485    string(25) "abcdefghijklmnopqrstuvwyz"
486    ["ALPHA"]=>
487    string(25) "ABCDEFGHIJKLMNOPQRSTUVWYZ"
488    ["digit"]=>
489    string(10) "0123456789"
490    ["special"]=>
491    string(31) "`1~!@#$%^&*()_+-={':[,]}|;.</>?"
492    ["hex"]=>
493    string(17) "ģ䕧覫췯ꯍ"
494    ["true"]=>
495    bool(true)
496    ["false"]=>
497    bool(false)
498    ["null"]=>
499    NULL
500    ["array"]=>
501    array(0) {
502    }
503    ["object"]=>
504    object(stdClass)#8 (0) {
505    }
506    ["address"]=>
507    string(19) "50 St. James Street"
508    ["url"]=>
509    string(20) "http://www.JSON.org/"
510    ["comment"]=>
511    string(13) "// /* <!-- --"
512    ["# -- --> */"]=>
513    string(1) " "
514    [" s p a c e d "]=>
515    array(7) {
516      [0]=>
517      int(1)
518      [1]=>
519      int(2)
520      [2]=>
521      int(3)
522      [3]=>
523      int(4)
524      [4]=>
525      int(5)
526      [5]=>
527      int(6)
528      [6]=>
529      int(7)
530    }
531    ["compact"]=>
532    array(7) {
533      [0]=>
534      int(1)
535      [1]=>
536      int(2)
537      [2]=>
538      int(3)
539      [3]=>
540      int(4)
541      [4]=>
542      int(5)
543      [5]=>
544      int(6)
545      [6]=>
546      int(7)
547    }
548    ["jsontext"]=>
549    string(49) "{"object with 1 member":["array with 1 element"]}"
550    ["quotes"]=>
551    string(27) "&#34; " %22 0x22 034 &#x22;"
552    ["/\"쫾몾ꮘﳞ볚
553
553	`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]=>
554    string(23) "A key can be any string"
555  }
556  [9]=>
557  float(0.5)
558  [10]=>
559  float(98.6)
560  [11]=>
561  float(99.44)
562  [12]=>
563  int(1066)
564  [13]=>
565  string(7) "rosebud"
566}
567DECODE AGAIN: AS ARRAY
568array(14) {
569  [0]=>
570  string(23) "JSON Test Pattern pass1"
571  [1]=>
572  array(1) {
573    ["object with 1 member"]=>
574    array(1) {
575      [0]=>
576      string(20) "array with 1 element"
577    }
578  }
579  [2]=>
580  array(0) {
581  }
582  [3]=>
583  array(0) {
584  }
585  [4]=>
586  int(-42)
587  [5]=>
588  bool(true)
589  [6]=>
590  bool(false)
591  [7]=>
592  NULL
593  [8]=>
594  array(31) {
595    ["integer"]=>
596    int(1234567890)
597    ["real"]=>
598    float(-9876.54321)
599    ["e"]=>
600    float(1.23456789E-13)
601    ["E"]=>
602    float(1.23456789E+34)
603    [""]=>
604    int(0)
605    ["zero"]=>
606    int(0)
607    ["one"]=>
608    int(1)
609    ["space"]=>
610    string(1) " "
611    ["quote"]=>
612    string(1) """
613    ["backslash"]=>
614    string(1) "\"
615    ["controls"]=>
616    string(5) "
617
617	"
618    ["slash"]=>
619    string(5) "/ & /"
620    ["alpha"]=>
621    string(25) "abcdefghijklmnopqrstuvwyz"
622    ["ALPHA"]=>
623    string(25) "ABCDEFGHIJKLMNOPQRSTUVWYZ"
624    ["digit"]=>
625    string(10) "0123456789"
626    ["special"]=>
627    string(31) "`1~!@#$%^&*()_+-={':[,]}|;.</>?"
628    ["hex"]=>
629    string(17) "ģ䕧覫췯ꯍ"
630    ["true"]=>
631    bool(true)
632    ["false"]=>
633    bool(false)
634    ["null"]=>
635    NULL
636    ["array"]=>
637    array(0) {
638    }
639    ["object"]=>
640    array(0) {
641    }
642    ["address"]=>
643    string(19) "50 St. James Street"
644    ["url"]=>
645    string(20) "http://www.JSON.org/"
646    ["comment"]=>
647    string(13) "// /* <!-- --"
648    ["# -- --> */"]=>
649    string(1) " "
650    [" s p a c e d "]=>
651    array(7) {
652      [0]=>
653      int(1)
654      [1]=>
655      int(2)
656      [2]=>
657      int(3)
658      [3]=>
659      int(4)
660      [4]=>
661      int(5)
662      [5]=>
663      int(6)
664      [6]=>
665      int(7)
666    }
667    ["compact"]=>
668    array(7) {
669      [0]=>
670      int(1)
671      [1]=>
672      int(2)
673      [2]=>
674      int(3)
675      [3]=>
676      int(4)
677      [4]=>
678      int(5)
679      [5]=>
680      int(6)
681      [6]=>
682      int(7)
683    }
684    ["jsontext"]=>
685    string(49) "{"object with 1 member":["array with 1 element"]}"
686    ["quotes"]=>
687    string(27) "&#34; " %22 0x22 034 &#x22;"
688    ["/\"쫾몾ꮘﳞ볚
689
689	`1~!@#$%^&*()_+-=[]{}|;:',./<>?"]=>
690    string(23) "A key can be any string"
691  }
692  [9]=>
693  float(0.5)
694  [10]=>
695  float(98.6)
696  [11]=>
697  float(99.44)
698  [12]=>
699  int(1066)
700  [13]=>
701  string(7) "rosebud"
702}
703