pass003.phpt 2.1 KB

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