You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

157 lines
3.1 KiB

  1. --TEST--
  2. Test json_encode() function : basic functionality
  3. --SKIPIF--
  4. <?php
  5. if (!extension_loaded("json")) {
  6. die('skip JSON extension not available in this build');
  7. }
  8. ?>
  9. --FILE--
  10. <?php
  11. /* Prototype : string json_encode ( mixed $value )
  12. * Description: Returns the JSON representation of a value
  13. * Source code: ext/json/php_json.c
  14. * Alias to functions:
  15. */
  16. echo "*** Testing json_encode() : basic functionality ***\n";
  17. //get an unset variable
  18. $unset_var = 10;
  19. unset ($unset_var);
  20. // get a resource variable
  21. $fp = fopen(__FILE__, "r");
  22. // get an object
  23. class sample {
  24. }
  25. $obj = new sample();
  26. $obj->MyInt = 99;
  27. $obj->MyFloat = 123.45;
  28. $obj->MyBool = true;
  29. $obj->MyNull = null;
  30. $obj->MyString = "Hello World";
  31. // array with different values for $string
  32. $inputs = array (
  33. // integers
  34. /*1*/ 0,
  35. 123,
  36. -123,
  37. 2147483647,
  38. -2147483648,
  39. // floats
  40. /*6*/ 123.456,
  41. 1.23E3,
  42. -1.23E3,
  43. // boolean
  44. /*9*/ TRUE,
  45. true,
  46. FALSE,
  47. false,
  48. // NULL
  49. /*13*/ NULL,
  50. null,
  51. // strings
  52. /*15*/ "abc",
  53. 'abc',
  54. "Hello\t\tWorld\n",
  55. // arrays
  56. /*18*/ array(),
  57. array(1,2,3,4,5),
  58. array(1 => "Sun", 2=>"Mon", 3 => "Tue", 4 => "Wed", 5 => "Thur", 6 => "Fri", 7 => "Sat"),
  59. array("Jan" => 31, "Feb" => 29, "Mar" => 31, "April" => 30, "May" => 31, "June" => 30),
  60. // empty data
  61. /*22*/ "",
  62. '',
  63. // undefined data
  64. /*24*/ @$undefined_var,
  65. // unset data
  66. /*25*/ @$unset_var,
  67. // resource variable
  68. /*26*/ $fp,
  69. // object variable
  70. /*27*/ $obj
  71. );
  72. // loop through with each element of the $inputs array to test json_encode() function
  73. $count = 1;
  74. foreach($inputs as $input) {
  75. echo "-- Iteration $count --\n";
  76. var_dump(json_encode($input));
  77. $count ++;
  78. }
  79. ?>
  80. ===Done===
  81. --EXPECTF--
  82. *** Testing json_encode() : basic functionality ***
  83. -- Iteration 1 --
  84. string(1) "0"
  85. -- Iteration 2 --
  86. string(3) "123"
  87. -- Iteration 3 --
  88. string(4) "-123"
  89. -- Iteration 4 --
  90. string(10) "2147483647"
  91. -- Iteration 5 --
  92. string(11) "-2147483648"
  93. -- Iteration 6 --
  94. string(7) "123.456"
  95. -- Iteration 7 --
  96. string(4) "1230"
  97. -- Iteration 8 --
  98. string(5) "-1230"
  99. -- Iteration 9 --
  100. string(4) "true"
  101. -- Iteration 10 --
  102. string(4) "true"
  103. -- Iteration 11 --
  104. string(5) "false"
  105. -- Iteration 12 --
  106. string(5) "false"
  107. -- Iteration 13 --
  108. string(4) "null"
  109. -- Iteration 14 --
  110. string(4) "null"
  111. -- Iteration 15 --
  112. string(5) ""abc""
  113. -- Iteration 16 --
  114. string(5) ""abc""
  115. -- Iteration 17 --
  116. string(18) ""Hello\t\tWorld\n""
  117. -- Iteration 18 --
  118. string(2) "[]"
  119. -- Iteration 19 --
  120. string(11) "[1,2,3,4,5]"
  121. -- Iteration 20 --
  122. string(72) "{"1":"Sun","2":"Mon","3":"Tue","4":"Wed","5":"Thur","6":"Fri","7":"Sat"}"
  123. -- Iteration 21 --
  124. string(58) "{"Jan":31,"Feb":29,"Mar":31,"April":30,"May":31,"June":30}"
  125. -- Iteration 22 --
  126. string(2) """"
  127. -- Iteration 23 --
  128. string(2) """"
  129. -- Iteration 24 --
  130. string(4) "null"
  131. -- Iteration 25 --
  132. string(4) "null"
  133. -- Iteration 26 --
  134. Warning: [json] (php_json_encode) type is unsupported, encoded as null in %s on line %d
  135. string(4) "null"
  136. -- Iteration 27 --
  137. string(82) "{"MyInt":99,"MyFloat":123.45,"MyBool":true,"MyNull":null,"MyString":"Hello World"}"
  138. ===Done===