Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85625 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 58323 invoked from network); 1 Apr 2015 06:58:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Apr 2015 06:58:17 -0000 Authentication-Results: pb1.pair.com smtp.mail=smalyshev@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=smalyshev@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.220.41 as permitted sender) X-PHP-List-Original-Sender: smalyshev@gmail.com X-Host-Fingerprint: 209.85.220.41 mail-pa0-f41.google.com Received: from [209.85.220.41] ([209.85.220.41:34724] helo=mail-pa0-f41.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B3/00-56894-9079B155 for ; Wed, 01 Apr 2015 01:58:17 -0500 Received: by pactp5 with SMTP id tp5so43452748pac.1 for ; Tue, 31 Mar 2015 23:58:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding; bh=mzXdEkATnCBntJLJg4WExDQtJMQGg0XFZDEAHelp434=; b=B8WwFFas5q69JGYP6gc95oMBo1AOg5CAR0DCKdx4HIiugktXO6Bo8IiW9YwV7JxoVK qZNp4lWq0ZWnu/Cw387tyDEz0vGMUEgyUkLjxc+aJLhu6QShI6ePMtE89XM6pOszA+db JQErP9BNeQ/w8Bdbic5yy2Eda08dMQbmvqt8RfCBXcwJsM7amIipnN97eSRt31fkdjdm hdHRyn3FywLBD4L+GmEKtHozorLaMtghFJvSgRu1p8plDjIs0A6n/Wf2tWGKdeyOec7F zEIV8krwcEmTSEPsgrB37PtGstkX6SYdTJKpbfFK94Qhyys9mJU7IytX56gPWPLRhpvk Oxtg== X-Received: by 10.68.129.39 with SMTP id nt7mr8396777pbb.72.1427871493809; Tue, 31 Mar 2015 23:58:13 -0700 (PDT) Received: from Stas-Air.local (108-66-6-48.lightspeed.sntcca.sbcglobal.net. [108.66.6.48]) by mx.google.com with ESMTPSA id x1sm1016324pdp.1.2015.03.31.23.58.12 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 31 Mar 2015 23:58:13 -0700 (PDT) Message-ID: <551B9704.4030302@gmail.com> Date: Tue, 31 Mar 2015 23:58:12 -0700 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:31.0) Gecko/20100101 Thunderbird/31.5.0 MIME-Version: 1.0 To: Jakub Zelenka , PHP internals list References: In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] JSON float number as string From: smalyshev@gmail.com (Stanislav Malyshev) Hi! > JSON_FLOAT_AS_STRING > decode: all float values will be decoded as string > - It's often an issue for very large float values with many fractional > digits that are coming from platforms that support larger float > representation than double. In that case the conversion is lost and there > is no way how to get it back (see http://bugs.php.net/68456 [pls ignore my > initial dump comments when I didn't get the issue :)] and an example of the > lost precision here http://3v4l.org/80iCh ). Converting the value to string > keep the precision and resolves the problem. This makes sense, though one should be careful as string and float is not the same and if you re-encode it, you will have data corruption (since if somebody using, say, Java, decodes it they'd have string where it expects float and decoding will likely fail). But I guess since it is an option, people that enable it will have to deal with it, e.g. by never re-encoding the data back. > encode: all float values will be encoded as string > - re-using the constant for encoder makes sense if PHP creates JSON for > platform that support lower float type (e.g. C float) and the precision > loss is not acceptable Here I'm not sure it makes much sense though. If it's float, it should be float. It's valid JSON, so if the receiving side can not handle it, it's their issue which they have to fix in their decoding. Just changing the types of data when encoding JSON does not sound like a good idea. Especially given that the reader may be some library like Java's Jackson, where types are very important. Of course, we could add tons of options like INT_AS_STRING, OBJECT_AS_STRING, etc. but I don't think this is what json encoder should be doing. > I think that this is more a bugfix as the precision is lost without any way I don't see how it is a bugfix. Representing floats is imprecise, it is known and not a bug. json_encode now produces completely valid representation of the data, so no bug there either. I don't think changing value types on encoding is a good idea. -- Stas Malyshev smalyshev@gmail.com