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