xref: /PHP-8.2/ext/json/tests/pass003.phpt (revision 32a1ebbd)
1--TEST--
2JSON (http://www.crockford.com/JSON/JSON_checker/test/pass3.json)
3--FILE--
4<?php
5
6$test = '
7{
8    "JSON Test Pattern pass3": {
9        "The outermost value": "must be an object or array.",
10        "In this test": "It is an object."
11    }
12}
13';
14
15echo 'Testing:' . $test . "\n";
16echo "DECODE: AS OBJECT\n";
17$obj = json_decode($test);
18var_dump($obj);
19echo "DECODE: AS ARRAY\n";
20$arr = json_decode($test, true);
21var_dump($arr);
22
23echo "ENCODE: FROM OBJECT\n";
24$obj_enc = json_encode($obj);
25echo $obj_enc . "\n";
26echo "ENCODE: FROM ARRAY\n";
27$arr_enc = json_encode($arr);
28echo $arr_enc . "\n";
29
30echo "DECODE AGAIN: AS OBJECT\n";
31$obj = json_decode($obj_enc);
32var_dump($obj);
33echo "DECODE AGAIN: AS ARRAY\n";
34$arr = json_decode($arr_enc, true);
35var_dump($arr);
36
37?>
38--EXPECTF--
39Testing:
40{
41    "JSON Test Pattern pass3": {
42        "The outermost value": "must be an object or array.",
43        "In this test": "It is an object."
44    }
45}
46
47DECODE: AS OBJECT
48object(stdClass)#%d (1) {
49  ["JSON Test Pattern pass3"]=>
50  object(stdClass)#%d (2) {
51    ["The outermost value"]=>
52    string(27) "must be an object or array."
53    ["In this test"]=>
54    string(16) "It is an object."
55  }
56}
57DECODE: AS ARRAY
58array(1) {
59  ["JSON Test Pattern pass3"]=>
60  array(2) {
61    ["The outermost value"]=>
62    string(27) "must be an object or array."
63    ["In this test"]=>
64    string(16) "It is an object."
65  }
66}
67ENCODE: FROM OBJECT
68{"JSON Test Pattern pass3":{"The outermost value":"must be an object or array.","In this test":"It is an object."}}
69ENCODE: FROM ARRAY
70{"JSON Test Pattern pass3":{"The outermost value":"must be an object or array.","In this test":"It is an object."}}
71DECODE AGAIN: AS OBJECT
72object(stdClass)#%d (1) {
73  ["JSON Test Pattern pass3"]=>
74  object(stdClass)#%d (2) {
75    ["The outermost value"]=>
76    string(27) "must be an object or array."
77    ["In this test"]=>
78    string(16) "It is an object."
79  }
80}
81DECODE AGAIN: AS ARRAY
82array(1) {
83  ["JSON Test Pattern pass3"]=>
84  array(2) {
85    ["The outermost value"]=>
86    string(27) "must be an object or array."
87    ["In this test"]=>
88    string(16) "It is an object."
89  }
90}
91