Req #68456 [ReO]: json_decode should be able to treat all numbers as strings

From: Date: Sun, 15 Mar 2015 21:17:21 +0000
Subject: Req #68456 [ReO]: json_decode should be able to treat all numbers as strings
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-191409@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=68456&edit=1 ID: 68456 User updated by: admin at bittylicious dot com Reported by: admin at bittylicious dot com Summary: json_decode should be able to treat all numbers as strings Status: Re-Opened Type: Feature/Change Request Package: JSON related Operating System: Linux Wheezy PHP Version: 5.4.35 Assigned To: bukka Block user comment: N Private report: N New Comment: Fab stuff, thanks so much for examining this. Great to hear that the encode functionality is also under consideration too. Previous Comments: ------------------------------------------------------------------------ [2015-03-15 20:06:28] bukka@php.net That makes sense. I'm a bit dump so it took me a bit... :) I actually recently closed something similar for encode so the constant could be used in both ways. I'd imagine something like JSON_DOUBLE_TO_STRING . I'll try to put together a quick RFC for 5.6 up (with choice for 7 only) next week and if it gets in, I will do the patch. ------------------------------------------------------------------------ [2015-03-15 19:42:53] admin at bittylicious dot com I do understand what you're saying, but my feature request is under the assumption you never ever need to convert this to a float, and my real life use case is indeed this situation. Simplifying things, let's imagine a JSON service that simply multiplies a number by two. You would pass in: {"val":1234567890.12345678} But you would have no way to even pass this to bcmul (which I believe works on strings) without losing precision even if bcmul needn't have any precision issues. Consider another service. This simply forwards "val" onwards perhaps with a hash signature or something. Consider this a "signing service". It cannot read this 'val' value without losing precision, when I think it should be possible to interpret this as a string. This is actually more similar to my use case, in other words, using JSON really as a transport mechanism and not manipulating these numbers within the PHP script. ------------------------------------------------------------------------ [2015-03-15 18:16:55] bukka@php.net The problem is that the precision will be lost anyway. Most of all you loose the precision immediately when you save it as a float. Basically this: $a = 1234567890.12345678; will loose the precision. You will be never able to get it back. I hope that this example will explain it a bit more: http://3v4l.org/r2uGa Mind that string conversion in json would do the same thing as using (string). Otherwise it wouldn't respect precision ini. Can you see what I mean now? ------------------------------------------------------------------------ [2015-03-15 17:27:02] admin at bittylicious dot com That's my point precisely though - I don't want to save the number as a float, I want to save it as a string. That way, I can either record it somewhere or do some bcmath on it. JSON is really regularly used as a transport stream where data isn't manipulated. In my real life example, I'm passing one value, via JSON, to a totally different process. I don't do any math on the number at all, but the precision is lost. This is why I want to treat it as a string but it's impossible to do this without manipulating the JSON itself before passing it to json_decode. Please reopen this if you agree. ------------------------------------------------------------------------ [2015-03-15 16:53:05] bukka@php.net The problem is that this number will never get saved with the full precision. You can influence the number of visible decimal places with setting ini precision as it is in http://3v4l.org/80iCh However as you can see the number is rounded because the internal float representation (C double type) cannot contain the whole number. So this is not JSON related. and any conversion to string can't fixed. The thing is that json_decode is using normal string conversion. So this can't be fixed in the JSON. I'd recommend you not to save the number to the the float at all but represent it as a string. ------------------------------------------------------------------------ The remainder of the comments for this report are too long. To view the rest of the comments, please view the bug report online at https://bugs.php.net/bug.php?id=68456 -- Edit this bug report at https://bugs.php.net/bug.php?id=68456&edit=1

« previous php.bugs (#191409) next »