Req #68456 [NEW]: json_decode should be able to treat all numbers as strings
| From: | admin at bittylicious dot com | Date: | Wed, 19 Nov 2014 22:18:57 +0000 |
| Subject: | Req #68456 [NEW]: json_decode should be able to treat all numbers as strings | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-188692@lists.php.net to get a copy of this message | ||
From: admin at bittylicious dot com
Operating system: Linux Wheezy
PHP version: 5.4.35
Package: JSON related
Bug Type: Feature/Change Request
Bug description:json_decode should be able to treat all numbers as strings
Description:
------------
There should be an option that can be passed to json_decode that will
allow numbers to be handled as strings. This is because some numbers can
not be represented in a float properly. In reality, JSON is often used
as a transport layer, and it's very useful to keep this accuracy.
One example in practice where this has caused issues is when dealing
with Dogecoin (a Bitcoin derivative). Dogecoin has eight decimal places,
and because each Dogecoin is worth only a tiny fraction of a cent, they
can often be represented as tens or hundreds of millions of Dogecoins.
When you're dealing with finance, it's essential not to lose precision.
The only workaround I have is to use a complicated preg_replace to add
quotes to the original JSON string, which is nasty.
Test script:
---------------
<?php
print_r(json_decode('{"test":1234567890.12345678}'));
?>
Output:
stdClass Object
(
[test] => 1234567890.1235
)
--
Edit bug report at https://bugs.php.net/bug.php?id=68456&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=68456&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=68456&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=68456&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=68456&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=68456&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=68456&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=68456&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=68456&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=68456&r=support
Expected behavior: https://bugs.php.net/fix.php?id=68456&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=68456&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=68456&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=68456&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=68456&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=68456&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=68456&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=68456&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=68456&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=68456&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=68456&r=mysqlcfg