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