xref: /php-src/ext/json/tests/bug50224.phpt (revision 32a1ebbd)
1--TEST--
2bug #50224 (json_encode() does not always encode a float as a float)
3--INI--
4serialize_precision=-1
5--FILE--
6<?php
7echo "* Testing JSON output\n\n";
8var_dump(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION));
9var_dump(json_encode(12, JSON_PRESERVE_ZERO_FRACTION));
10var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));
11var_dump(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION));
12var_dump(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION));
13var_dump(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION));
14
15echo "\n* Testing encode/decode symmetry\n\n";
16
17var_dump(json_decode(json_encode(12.3, JSON_PRESERVE_ZERO_FRACTION)));
18var_dump(json_decode(json_encode(12, JSON_PRESERVE_ZERO_FRACTION)));
19var_dump(json_decode(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION)));
20var_dump(json_decode(json_encode(0.0, JSON_PRESERVE_ZERO_FRACTION)));
21var_dump(json_decode(json_encode(array(12, 12.0, 12.3), JSON_PRESERVE_ZERO_FRACTION)));
22var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION)));
23var_dump(json_decode(json_encode((object)array('float' => 12.0, 'integer' => 12), JSON_PRESERVE_ZERO_FRACTION), true));
24?>
25--EXPECTF--
26* Testing JSON output
27
28string(4) "12.3"
29string(2) "12"
30string(4) "12.0"
31string(3) "0.0"
32string(14) "[12,12.0,12.3]"
33string(27) "{"float":12.0,"integer":12}"
34
35* Testing encode/decode symmetry
36
37float(12.3)
38int(12)
39float(12)
40float(0)
41array(3) {
42  [0]=>
43  int(12)
44  [1]=>
45  float(12)
46  [2]=>
47  float(12.3)
48}
49object(stdClass)#%d (2) {
50  ["float"]=>
51  float(12)
52  ["integer"]=>
53  int(12)
54}
55array(2) {
56  ["float"]=>
57  float(12)
58  ["integer"]=>
59  int(12)
60}
61