Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:85839 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 91951 invoked from network); 16 Apr 2015 18:04:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 16 Apr 2015 18:04:32 -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.223.178 as permitted sender) X-PHP-List-Original-Sender: jakub.php@gmail.com X-Host-Fingerprint: 209.85.223.178 mail-ie0-f178.google.com Received: from [209.85.223.178] ([209.85.223.178:32975] helo=mail-ie0-f178.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 00/A7-39957-EA9FF255 for ; Thu, 16 Apr 2015 14:04:30 -0400 Received: by iecrt8 with SMTP id rt8so45193615iec.0 for ; Thu, 16 Apr 2015 11:04:27 -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=ZMu/2C/sLE4822wza7yPUakInun/58S2oHG1pcpuxYw=; b=lHJnLDrig0oHSnnsaQObew19aXb5ptRkHGmBdX0yFQWE2HdfTLy74I1RtMpIyk5lTX q/SJaEPvuOP5a4tm710pMCM9khMCijuSpM3HGxIm/r2Cn9HcpkXrIgdefGLlkru/aCBx faMUiULguJGhg+wDT5h6MyzzbyjoB3yV+haJmaUxPa8kBDwJYNGeIqzMhj0r0W53gyjY JHcEhel4hXLFhfgwG8HSE4Id23BhS9buH5zGwpx8J2Yt7GkKY5yDmh1y9+FKghFBkku8 fZ/FsYeybzHBVQK0nKFI/LGlzymCFFXPpXUjX1zTNVkmgfrUcSblcXbBjUg+nVYzoxGV 14wA== MIME-Version: 1.0 X-Received: by 10.50.176.137 with SMTP id ci9mr2487082igc.2.1429207467694; Thu, 16 Apr 2015 11:04:27 -0700 (PDT) Sender: jakub.php@gmail.com Received: by 10.107.39.18 with HTTP; Thu, 16 Apr 2015 11:04:27 -0700 (PDT) In-Reply-To: <4D090060-127D-4BA8-8C98-6E315EF077C0@gmail.com> References: <4D090060-127D-4BA8-8C98-6E315EF077C0@gmail.com> Date: Thu, 16 Apr 2015 19:04:27 +0100 X-Google-Sender-Auth: -niuCqPg1q7ICAjg04oLYSmH0FU Message-ID: To: Alexey Zakhlestin Cc: PHP internals list Content-Type: multipart/alternative; boundary=089e0111e0da6c1e740513db4b1a Subject: Re: [PHP-DEV] JSON float number as string From: bukka@php.net (Jakub Zelenka) --089e0111e0da6c1e740513db4b1a Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hi, On Tue, Apr 14, 2015 at 7:33 AM, Alexey Zakhlestin wrote: > > > On 14 Apr 2015, at 07:31, Alexey Zakhlestin wrote: > > > > Feels a bit hackish > > I think it is possible to introduce an overall better solution > > > > We can expose result of json-tokenizing as a tree of objects: > > > > JSON\Object > > JSON\Array > > JSON\String > > JSON\Number > > JSON\False > > JSON\True > > JSON\Null > > > > then, it would be possible to introduce any number of experimental > userland implementations like the one proposed here > > I decided to elaborate > > namespace JSON; > > abstract class Value > { > public function toJson(); > public function toNative(); // this would do, whatever > json_decode does > } > > > class Object extends Value implements \ArrayAccess, \IteratorAggregat= e > { > } > > class Array extends Value implements \ArrayAccess, \IteratorAggregate > { > } > > abstract class Literal extends Value > { > public function toString(); > public function __toString() > { > return $this->toString(); > } > } > > class String extends Literal > { > } > > class Number extends Literal > { > public function toInt(); > public function toFloat(); > } > > class False extends Literal > { > } > > class True extends Literal > { > } > > class Null extends Literal > { > } > > > So, in case of Number, there would be a way to get the value the way it > was stored in document using toString(), use toInt() or toFloat() to get = a > value with possible precision loss or use toNative(), which would be > =E2=80=9Csmart=E2=80=9D and return int or float using the same logic, whi= ch json_decode() > uses now. > > It would work the other way round too. Object::toJson() would return > json-representation of the tree. > This could of course can't be introduced as a default (at least not in 7) as it's a huge BC break. I don't like introducing too many new flags either (it might be the case that you would like to use this just for objects but not for Literals). Also it can't be mixed (meaning you can't have some values as int and some as Json\Number). However I think this could work nicely with json-scheme type hints and using that only if explicitly specified in the scheme. The only problem is that it's not a small feature so it will probably take some time to make that happen. JSON_FLOAT_AS_STRING is meant like a quick fix for something that is causing issue. However I it's a bit "hackish" as it applies on all floats which is not very nice. Maybe it will really make sense to wait for json-scheme. More I look into more I think that it would be a better solution. Cheers Jakub --089e0111e0da6c1e740513db4b1a--