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