Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:87299 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 7437 invoked from network); 26 Jul 2015 18:32:33 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 26 Jul 2015 18:32:33 -0000 Authentication-Results: pb1.pair.com header.from=jakub.php@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=jakub.php@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.213.171 as permitted sender) X-PHP-List-Original-Sender: jakub.php@gmail.com X-Host-Fingerprint: 209.85.213.171 mail-ig0-f171.google.com Received: from [209.85.213.171] ([209.85.213.171:38566] helo=mail-ig0-f171.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2D/00-06606-DB725B55 for ; Sun, 26 Jul 2015 14:32:30 -0400 Received: by iggf3 with SMTP id f3so52113532igg.1 for ; Sun, 26 Jul 2015 11:32:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=UPZ868tCSLB0mELQDZX4Tgje5ya1cEicTFmtLLKev7A=; b=eoAFt0IZcQc42tUMOxa1S93QKo8CbEmvRd6hM+JctbSFHQxEn5T1Woxu8nfQyplfbP Te/CTrNqxKG6YnlW1A8Lt9zuptsy8tHlNzBJna2xJo3bkMohjxUNPPYLPWNjt1fAByrk oi+Y7zjrtvP+IdF6tTVcnSxpup9KK+J66nYiWMxjlKa0hAYhzI91YIA3saws+y0eP2B3 FGpvcWKUlaH53cFMJeUdORf/ak2LNT9HNxziPIYvbqimObON2lxbG4NkopJbpR/r1adE puhMiYb3ZsiFD3vVon63HCE5R7XeKI0H9JRzRxgJznEtC41AH9JbzV37qjWMIY0a3mfj 0QsQ== MIME-Version: 1.0 X-Received: by 10.107.138.13 with SMTP id m13mr40680744iod.24.1437935546268; Sun, 26 Jul 2015 11:32:26 -0700 (PDT) Sender: jakub.php@gmail.com Received: by 10.107.20.68 with HTTP; Sun, 26 Jul 2015 11:32:26 -0700 (PDT) In-Reply-To: References: Date: Sun, 26 Jul 2015 19:32:26 +0100 X-Google-Sender-Auth: MLia3dyDwpVT1e3kpzVPZDZLWYE Message-ID: To: Yasuo Ohgaki Cc: "internals@lists.php.net" Content-Type: multipart/alternative; boundary=001a113f1ef27205b5051bcb7514 Subject: Re: [PHP-DEV] json_decode/encode should return full precision values by default From: bukka@php.net (Jakub Zelenka) --001a113f1ef27205b5051bcb7514 Content-Type: text/plain; charset=UTF-8 Hi, On Sat, Jul 25, 2015 at 11:01 PM, Yasuo Ohgaki wrote: > Hi all, > > I had to work with Google Map API and need to handle values precisely. > Fortunately, it seems values are IEEE double, but I get rounded float > values by default. > > For example, > $json = ' > { > "results" : [ > { > "elevation" : 1608.637939453125, > "location" : { > "lat" : 39.73915360, > "lng" : -104.98470340 > }, > "resolution" : 4.771975994110107 > }, > { > "elevation" : -50.78903579711914, > "location" : { > "lat" : 36.4555560, > "lng" : -116.8666670 > }, > "resolution" : 19.08790397644043 > } > ], > "status" : "OK" > } > '; > > var_dump(json_decode($json)); > ?> > > object(stdClass)#5 (2) { > ["results"]=> > array(2) { > [0]=> > object(stdClass)#1 (3) { > ["elevation"]=> > float(1608.6379394531) > ["location"]=> > object(stdClass)#2 (2) { > ["lat"]=> > float(39.7391536) > ["lng"]=> > float(-104.9847034) > } > ["resolution"]=> > float(4.7719759941101) > } > [1]=> > object(stdClass)#3 (3) { > ["elevation"]=> > float(-50.789035797119) > ["location"]=> > object(stdClass)#4 (2) { > ["lat"]=> > float(36.455556) > ["lng"]=> > float(-116.866667) > } > ["resolution"]=> > float(19.08790397644) > } > } > ["status"]=> > string(2) "OK" > } > > > json_decode()/json_encode() must be able to handle precise IEEE double > value by _default_. > > serialize() is changed to use max precision. json_decode/encode should do > the same at least. > > I think this change should be provided as bug fix. > Any comments? > > I don't think that this is a bug. Your example is also completely unrelated to json because the place when the value is rounded is var_dump where it's based on ini precision. You would get the same values with json_encode but it's only because it uses the same ini ( precision ). Basically it has nothing to do with json_decode. I see that that the bug report you created ( https://bugs.php.net/bug.php?id=70137 ) is actually about using serialize.precision instead of precision in json_encode . I agree that using 'precision' ini is not the best solution. However I don't think that start suddenly using serialize.precision is a good idea. First of all it's a big BC break that will be quite difficult to find (There is no way this should go to the point release because this is changing the generated json). Also the json encode usage is much wider than serialization. There might be use cases when you don't care about maximal precision and you prefer to save some space. Imagine that you are transferring lots of float numbers. Then it will result in increasing of the transfer size. It would be great if the new ini was used when it was introduced but I'm afraid that changing that now is not a good idea I think that due to BC break, this shouldn't be changed. If you really want a bigger precision, you can do ini_set('precision', 30) before json_encode and then set it back. It means that this not a bug because you can still change it if you want. That means that we can't change before PHP 8 or whatever comes after 7... :) Cheers Jakub --001a113f1ef27205b5051bcb7514--