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